Help required on MySQLi query
Hi All,
I need some help in creating a MySQLi query. I have a table in which feedbacks are submitted. I want to create a query which will count all the feedbacks for a specific date and then display the results for each user for that particular date. For example if I (tl0tr) has submitted 4 feedbacks and another user has submitted 8 feedbacks on 21-Mar-2016. Then the result should display something like mentioned below. I am creating this in PHP. I am not able to figure out the correct query to show my result something similar to this.
Username Count
tl0tr 4
Other_User 8
Edit:
I found the query
SELECT username, count(*) FROM `feedbacks` WHERE `createdate` = '2016-03-18' group by username
Though this works perfectly fine in SQL I am not able to make it work using PHP. Any help would be greatly appreciated.
I can’t test my code by now, but maybe it can be useful if I understand your method and the code are corect. I will test the code when I can and correct this if is needed.
$sql = "SELECT username, COUNT(*) as total FROM `feedbacks` WHERE `createdate` = '2016-03-18' GROUP BY username";
$results = mysqli_query($connect, $sql);
foreach($results as $result) {
echo $result[****'username'].' '.$result[****'total'];
}
?
In the MySQLi docs, they say that you must compile PHP with support for the MySQLi extension.
more information there:
http://www.w3schools.com/php/php_ref_mysqli.asp
What about PDO for querying?
http://php.net/manual/fr/class.pdo.php
Cheers
LINUX is simple, it just takes a genius to understand its simplicity
Dennis_Ritchie
@bolofecal : The query works perfectly. Thank you so much. I am trying to put that into a table format to display the results but unable to do it. Is there way that I can move that foreach down in the HTML. for example :
<table>
<tr>
<td>Username</td>
<td>Feedback Count</td>
</tr>
<?php foreach($results as $result) { ?>
<tr>
<td>Result of username</td>
<td>Result of Feedback Count</td>
</tr>
<?php } ?>
</table>
If I understand correctly you try put the results inside a table:
<table>
<tr>
<td>Username</td>
<td>Feedback Count</td>
</tr>
<?php
foreach($results in $result) {
echo '<tr><td>'.$result[****'username'].'</td><td>'.$result[****'total'].'</td></tr>';
}
?>
</table>
?
@bolofecal: *as
This should work as well:
<table>
<tr>
<td>Username</td>
<td>Feedback Count</td>
</tr>
<?php foreach ($results as $result) { ?>
<tr>
<td><?= $result[**'username'] ?></td>
<td><?= $result[**'total'] ?></td>
</tr>
<?php } ?>
</table>
@dloser, I see that you use php short tags in your code, what’s your opinion about using them?
I always avoided using them on all my projects but as of php >=5.4 there has been some changes, but I still never use them.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘\’‘ at line 1
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘\’‘ at line 1
Thank you @bolofecal , @dloser and everyone else for helping me with this. I was able to make it work. Closing this thread. Thank you once again.