bind_param(): 变量数与准备语句中的参数数不匹配

bind_param(): Number of variables doesn't match number of parameters in prepared statement

基本上,问题是我的数据库中有一列日期 table,我想计算每个特定日期的数量并将其存储在 array.I 中,这让我大吃一惊围绕这个问题一个星期,到目前为止我已经想出了这个。

<?php

function vref($arr) {
 if (strnatcmp(phpversion(),'5.3') >= 0) {
  $refs = array();
  foreach($arr as $key => $value) $refs[$key] = &$arr[$key];
  return $refs;
 }
 return $arr;
}

$mysqli = new mysqli("localhost", "root","" , "ti_project");

$bind = 'sssss';
$feedbackdate = array($bind);

$query = "SELECT dateTime FROM feedback";
$result = $mysqli->prepare($query);
$result->execute();
$result->bind_result($Date);
while ($result->fetch()){
    $feedbackdate[] = array($Date);
}

$rawQuery = 'SELECT COUNT(*) FROM feedback WHERE dateTime IN (';
$rawQuery .= implode(',',array_fill(0,count($feedbackdate),'?'));
$rawQuery .= ')';

$stmt = $mysqli->prepare($rawQuery);
call_user_func_array(array($stmt,'bind_param'),vref($feedbackdate));
$stmt->execute();
$stmt->bind_result($count);

while ($stmt->fetch()) {
    printf ("%s\n", $count);
}

?>

但是这里我得到了错误

mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement.

So how to do this?

你的参数数组由数组组成,我认为这不是你想要的:

替换:

while ($result->fetch()){
    $feedbackdate[] = array($Date);
}

作者:

while ($result->fetch()){
    $feedbackdate[] = $Date;
}

此外,您已经用一个元素初始化了数组,这毫无意义。所以替换:

$feedbackdate = array($bind);

与:

$feedbackdate = array();

然后使用 execute():

的可选参数可以更容易地注入这些参数
$stmt = $mysqli->prepare($rawQuery);
$stmt->execute($feedbackdate);

我不确定为什么您需要执行两次查询才能获得您要查找的结果集。此查询将按日期对结果进行分组并对其进行计数:

SELECT dateTime, COUNT(*) FROM feedback GROUP BY dateTime;

这将输出如下内容:

+-----------------------+-------+
| dateTime              | count |
+-----------------------+-------+
|2016-01-25 00:00:00    | 1     |
|2016-01-24 00:00:00    | 2     |
+-----------------------+-------+

这是您想要的数据类型吗?