为什么在查询中设置 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()?

我有以下查询:

$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();

这一切都很好!

问题

但是,当我尝试在查询语句的开头输入 SET SESSION group_concat_max_len = 1000000; 时,出现以下错误:

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

我知道我的查询现在有问题,但是当将它复制粘贴到我的 DBMS 时,可以毫无问题地执行查询。

我做错了什么?

您的问题是您试图同时执行两个查询,而 mysqli::prepare 不支持,因此它失败并且 returns 错误。相反,运行 首先将变量设置为单独的查询:

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

请注意,您应该检查通话状态,就像我在上面的代码中所做的那样。