简体   繁体   English

为什么在查询中设置group_concat_max_len变量会导致PHP的bind_param()出错?

[英]Why setting the group_concat_max_len variable in a query results in an error in PHP's bind_param()?

I have the following query: 我有以下查询:

$query = <<<SQL
SELECT
      year,  
      count(*) AS `counter`, 
      GROUP_CONCAT(team) AS `team_list` 
    FROM
      team_list 
    WHERE year IS NOT NULL              
SQL;

if (!empty($sql)) { //$sql is an array of SQL WHERE statements "a IN (a,b,c)"
    $query .= ' AND ' . implode(' AND ', $sql);
} 


    $query .= 'GROUP BY year ORDER BY year';


    /////////////////////////////
    //EXECUTING THE QUERIES
    /////////////////////////////

    //Filter count to know how many 's' variable have to be bind to the prepared statement
    $filterCount = count($teams) + count($countries) + count($years) + count($rankings); //These are my ajax elements that are also used in the $sql variable

    //Data query
    $queryYears = $connection->prepare($query);
    $queryYears->bind_param(str_repeat('s', $filterCount), ...$teams, ...$countries, ...$years, ...$rankings);
    $queryYears-> execute();

This all works very fine! 这一切都很好!

THE PROBLEM 问题

However, once I try to enter SET SESSION group_concat_max_len = 1000000; 但是,一旦我尝试输入SET SESSION group_concat_max_len = 1000000; at the beginning of my query statement I get the following error: 在我的查询语句的开头我得到以下错误:

Fatal error: Uncaught Error: Call to a member function bind_param() on boolean

I understand that something is now wrong with my query, but when copy-pasting it to my DBMS the query can be executed without a problem. 我知道我的查询现在出现了问题,但是当将其复制粘贴到我的DBMS时,查询可以毫无问题地执行。

What am I doing wrong here? 我在这做错了什么?

Your problem is that you are trying to execute two queries at once, and mysqli::prepare doesn't support that, so it fails and returns false. 你的问题是你试图一次执行两个查询,而mysqli::prepare不支持,所以它失败并返回false。 Instead, run the variable set as a separate query first: 而是首先将变量集作为单独的查询运行:

$connection->query("SET SESSION group_concat_max_len = 1000000;") or die($connection->error);
$queryYears = $connection->prepare($query) or die($connection->error);
// etc.

Note that you should be checking the status of your calls, as I have done in the code above. 请注意,您应该检查您的通话状态,就像我在上面的代码中所做的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM