PDOException:SQLSTATE [IMSSP]:尝试绑定参数号65536.SQL Server最多支持2100个参数

时间:2017-05-04 23:54:19

标签: php sql-server pdo

好的,所以我遇到了一个非常奇怪的PDO异常,我似乎无法理解。以下是生成的异常:

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会抛出异常?

1 个答案:

答案 0 :(得分:5)

我花了几分钟才看到我做错了什么,但是很快就点击了我试图将product_type_id绑定到一个占位符,该占位符在调用的查询中不存在第161行,但仍存在于第166行的查询中。因此,如果$this->product_type_id等于999,则由于尝试绑定到第161行的查询,PDO会抛出异常,但在任何其他时间它会工作正常,因为它会尝试在第166行运行查询。这需要进行轻微的代码调整,如下所示:

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

对于每个条件,查询都已准备好。然后,如果调用第二个查询而不是第一个查询,它将仅在该点绑定参数。

相关问题