Help required on MySQLi query

tl0tr
8 years ago | edited 8 years ago

0

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.

15replies
7voices
280views
1image
? [bolofecal]
8 years ago | edited 8 years ago

1

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']; }

dloser
8 years ago

0

What do you mean with “I am not able to make it work using PHP”? What is the problem you are having or errors you are getting?

(Also, what is a “MySQLi query”?)

ITPPA
8 years ago

0

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

tl0tr
8 years ago

0

@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>  
? [bolofecal]
8 years ago | edited 8 years ago

0

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>

Reply has been removed
dloser
8 years ago

1

@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>  
? [bolofecal]
8 years ago

0

Great, this is more clear, @dloser ever reduces the code :)

Mr. Cyph3r [MrCyph3r]
8 years ago

0

@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.

dloser
8 years ago

0

I don’t really have a big opinion. Whether you use them or not, it’s still PHP. ;)

In general I’d say whatever is more readable in that case. Unless there is a very significant performance penalty.

Mr. Cyph3r [MrCyph3r]
8 years ago

0

Hmm I see, never really liked them. It’s probably a “religious” thing like spaces vs tabs… I was just curious.

Mugi [Mugiwara27]
8 years ago | edited 8 years ago

0

DId someone say… “Spaces vs tabs” ?
Whyyy using spaces ? :(

dloser
8 years ago

0

Why? Because the size of space is constant while a tab’s depends on settings. Tabs suck.

Mr. Cyph3r [MrCyph3r]
8 years ago

0

Same here, spaces. I always remap tabs to spaces.
And it avoids me headaches on multi-line indentation, expecially when working with other devs, I’ll never switch back to tabs.

Mugi [Mugiwara27]
8 years ago

0

So much hate for tabs wow
I’ll use spaces now lol :)

tl0tr
8 years ago

0

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.

Discussion thread has been locked. You can no longer add new posts.
1 of 16

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss