gpt4 book ai didi

php - PDOException : SQLSTATE[IMSSP]: Tried to bind parameter number 65536. SQL Server 最多支持2100个参数

转载 作者:可可西里 更新时间:2023-10-31 22:51:08 28 4
gpt4 key购买 nike

好吧,我遇到了一个非常奇怪的 PDOException,我似乎无法理解。这是生成的异常:

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

最佳答案

我花了几分钟才看到我做错了什么,但很快就发现我正在尝试将 product_type_id 绑定(bind)到一个占位符,而这个占位符在被调用的查询中不存在在第 161 行,但存在于第 166 行的查询中。因此,如果 $this->product_type_id 等于 999,PDO 将抛出异常,因为尝试绑定(bind)到第 161 行的查询,但任何其他时间它都可以正常工作,因为它会尝试运行第 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...

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

关于php - PDOException : SQLSTATE[IMSSP]: Tried to bind parameter number 65536. SQL Server 最多支持2100个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43794441/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com