2017-05-04 194 views
2

これで、頭がおかしくなるようなPDOExceptionが発生しました。ここで生成された例外は次のとおりです。PDOException:SQLSTATE [IMSSP]:パラメーター番号65536をバインドしようとしました。SQL Serverは最大2100個のパラメーターをサポートしています。

PDOException: SQLSTATE[IMSSP]: Tried to bind parameter number 65536. SQL Server supports a maximum of 2100 parameters. in D:\Work\CEUR16-004\Project\www_root\includes\scripts\php\libCore\products.php:169 
Stack trace: 
#0 D:\Work\CEUR16-004\Project\www_root\includes\scripts\php\libCore\products.php(169): PDOStatement->execute() 
#1 D:\Work\CEUR16-004\Project\www_root\includes\pages\products\products.php(5): ProductCore->build_product_list() 
#2 D:\Work\CEUR16-004\Project\www_root\index.php(27): include('D:\\Work\\CEUR16-...') 
#3 {main} 

そして、ここではそれが参照するコードさ:$this->product_type_id999に等しいとき

public function build_product_list() 
    { 
     // This function builds the product list visible on the main site (guests only) 
     try 
     { 
      if($this->product_type_id == "999") 
      { 
       $query = 'SELECT sp.product_id, sp.product_name, sp.product_price, pt.product_type_name FROM shop_products AS sp LEFT JOIN product_types AS pt ON sp.product_type_id = pt.product_type_id ORDER BY sp.product_type_id ASC'; // Line 161 
      } 
      else 
      { 
       $query = 'SELECT sp.product_id, sp.product_name, sp.product_price, pt.product_type_name FROM shop_products AS sp LEFT JOIN product_types AS pt ON sp.product_type_id = pt.product_type_id WHERE sp.product_type_id = :product_type_id ORDER BY sp.product_id ASC'; 
      } 
      $stmt = $this->dbcore_prod_core->dbc->prepare($query); 
      $stmt->bindParam(':product_type_id', $this->product_type_id, PDO::PARAM_INT); 
      $stmt->execute(); // Line 169 
      // Function continues below... 

は今、この例外がのみ生成されるライン161上のクエリを実行します(上記のコードで注釈が付けられています)。私はサーバ上で直接クエリを実行し、期待される結果を返します。なぜPDOが例外をスローしますか?

答えて

2

私が間違っていることを確認するのに数分かかりましたが、すぐに、ライン161で呼び出されたクエリに存在しないプレースホルダにバインドしようとしていました。したがって、$this->product_type_id999に等しい場合、PDOは、161行目のクエリへのバインドを試行したために例外をスローしますが、それ以外のときはクエリを実行しようとしているため正常に動作しますこれは、次のようにわずかなコード調整を必要としました。

public function build_product_list() 
    { 
     // This function builds the product list visible on the main site (guests only) 
     try 
     { 
      if($this->product_type_id == "999") 
      { 
       $query = 'SELECT sp.product_id, sp.product_name, sp.product_price, pt.product_type_name FROM shop_products AS sp LEFT JOIN product_types AS pt ON sp.product_type_id = pt.product_type_id ORDER BY sp.product_type_id ASC'; // Line 161 
       $stmt = $this->dbcore_prod_core->dbc->prepare($query); 
      } 
      else 
      { 
       $query = 'SELECT sp.product_id, sp.product_name, sp.product_price, pt.product_type_name FROM shop_products AS sp LEFT JOIN product_types AS pt ON sp.product_type_id = pt.product_type_id WHERE sp.product_type_id = :product_type_id ORDER BY sp.product_id ASC'; 
       $stmt = $this->dbcore_prod_core->dbc->prepare($query); 
       $stmt->bindParam(':product_type_id', $this->product_type_id, PDO::PARAM_INT); 
      } 
      $stmt->execute(); // Line 169 
      // Function continues below... 

各条件について、クエリが準備されます。次に、2番目のクエリが最初のクエリの代わりに呼び出されると、その時点でのみパラメータがバインドされます。

関連する問題