2017-05-04 876 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} 

,這裏是它指的是代碼:現在

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... 

,當$this->product_type_id等於999運行於線161查詢(只產生此異常在上面的代碼中註釋)。我直接在服務器上運行查詢,並返回預期的結果,那麼爲什麼PDO會引發異常?

回答

2

我花了幾分鐘的時間來看看我做錯了什麼,但是很快就點擊了我試圖將product_type_id綁定到一個佔位符,該佔位符在第161行調用的查詢中不存在,但存在所以如果$this->product_type_id等於999,由於試圖綁定到第161行的查詢,PDO會拋出一個異常,但是任何其他時間它都可以正常工作,因爲它會嘗試運行查詢這需要稍微調整代碼,如下所示:

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... 

對於每種情況,都會準備查詢。然後如果第二個查詢被調用而不是第一個查詢,它將只綁定那個點的參數。