Jump to content

pdo count where


Go to solution Solved by Barand,

Recommended Posts

I am still working on learning pdo   slowly trying to migrate into the  newest  most secure operations  and have an issue with the count and no idea where to look for it  i have spent countless hours searching and am no further along so i thought id ask the people here if this is even possible

 

What i have is a count  total number of entries  per person which i have working perfectly  but the issue is i need to only count them when the  total is greater then 5

$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);  
 foreach($dbh->query('SELECT username,COUNT(*), WHERE COUNT(*)<5 
 FROM games  
GROUP BY username') as $row) {  
echo "<tr>";  
echo "<td>" . $row['username'] . "</td>";  
echo "<td>" . $row['COUNT(*)'] . "</td>";  
echo "</tr>";   
}  
?>  

so if a user only has 4 entries it will skip his name

I hope this makes sense and hope someone can help me

Link to comment
https://forums.phpfreaks.com/topic/302955-pdo-count-where/
Share on other sites

A WHERE condition filters inputs to the query. At that time the totals aren't known. To filter output, use HAVING

SELECT username,COUNT(*) as total, 
FROM games
GROUP BY username
HAVING total > 5
Edited by Barand
Link to comment
https://forums.phpfreaks.com/topic/302955-pdo-count-where/#findComment-1541507
Share on other sites

when i tried that with all the code it give me an Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\pawn\listtest.php on line 59

but if i i remove the having it  works no problem  here is the updated code

$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);  
 foreach($dbh->query('SELECT username,COUNT(*) as total, 
FROM games
GROUP BY username
HAVING total > 5') as $row) {  
echo "<tr>";  
echo "<td>" . $row['username'] . "</td>";  
echo "<td>" . $row['COUNT(*)'] . "</td>";  
echo "</tr>";   
}  
?>  
Link to comment
https://forums.phpfreaks.com/topic/302955-pdo-count-where/#findComment-1541516
Share on other sites

  • Solution

Syntax error in the query. Remove the comma after "as total".

 

As COUNT(*) now has the column alias "total" you should refer to it as $row['total'] and not $row['COUNT(*)'];

 

It's easier to give expressions like that a column alias, especially for complex expressions.

Link to comment
https://forums.phpfreaks.com/topic/302955-pdo-count-where/#findComment-1541519
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.