1

Basically the problem is I have a column of dates in my database table and I want to count the number of each particular date and have it stored in an array.I've busted my head around this problem for a week and so far I've come up with this.

<?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);
}

?>

But here I get the error

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

So how to do this?

Saeesh
  • 15
  • 3

2 Answers2

0

Your parameter array consists of arrays, which I don't think is what you want:

Replace:

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

By:

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

Also, you initialise your array already with one element, which makes no sense. So replace:

$feedbackdate = array($bind);

with:

$feedbackdate = array();

Then the injection of these arguments can be done a lot easier by using the optional parameter of execute():

$stmt = $mysqli->prepare($rawQuery);
$stmt->execute($feedbackdate);
trincot
  • 317,000
  • 35
  • 244
  • 286
  • why will I not want an array? Date is a column and there are multiple dates and i want to count those dates. Multiple dates hence array – Saeesh Jan 25 '16 at 16:53
  • Because it would be an array of array as you did it, like `[ ['2016-01-18'], ['2016-01-18'], ...]`, while you need `[ '2016-01-18', '2016-01-18', ...]` – trincot Jan 25 '16 at 16:55
  • oh now i get it. Thanks – Saeesh Jan 25 '16 at 16:59
  • Ah yes, you need to replace this as well: `$feedbackdate = array($bind);` -- remove `$bind`. I added that in my answer. Not sure what you wanted to do here. – trincot Jan 25 '16 at 17:02
  • Oh no actually $bind is a string which contains the type of parameters i'm binding.Its needed in the array as the first element. – Saeesh Jan 25 '16 at 17:24
  • I see, but skip it anyway, because `s` is the default. As you had it, that argument was not arriving where you wanted it anyway. The type has to be passed as third argument to *bindParam()*. Anyway, leave it out. – trincot Jan 25 '16 at 17:40
0

I am not sure why you need to do two queries to get the result set you are looking for. This query will group the results by date and count them:

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

This will output something like:

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

Is that the type of data you are after?

HenryTK
  • 1,287
  • 8
  • 11