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

时间:2019-05-07 04:40:06

标签: php mysql ajax sql-server-group-concat

我有以下查询:

$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时,可以毫无问题地执行查询。

我在这里做什么错了?

1 个答案:

答案 0 :(得分:3)

您的问题是您试图一次执行两个查询,而mysqli::prepare不支持该查询,因此它失败并返回false。相反,请先将变量集作为单独的查询运行:

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

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