Jump to content

SELECT COUNT


WatsonN

Recommended Posts

I'm trying to figure out how to implement SELECT COUNT into a php generated list.

My script lists all the users in one table

$data = mysql_query("SELECT * FROM YBK_Login") or die(mysql_error()); 
while($info = mysql_fetch_array( $data )) { 
Print "<tr>"; 
Print "<td>".$info['UID'] . " </td>";
Print "<td>"."#"."</td>";
Print "<td>".$info['ID'] . "</td> "; 
Print "<td>".$info['Allowed'] . "</td> ";
Print "<td>".$info['pass'] . " </td>";
Print "<td>".$info['HR'] . " </td>"; } 

?>

 

And I want to use the count to count the number of entries each user has in a diffrent table.

Like pull the $info['ID'] and check against posts in YBK_Ads with the column name UID

Link to comment
https://forums.phpfreaks.com/topic/215513-select-count/
Share on other sites

A subquery should work?

 

$data = mysql_query("SELECT a.*, (SELECT COUNT(posts) FROM YBK_Ads a WHERE a.UID = y.UID) AS postCount FROM YBK_Login y") or die(mysql_error()); 
while($info = mysql_fetch_array( $data )) { 



Print "<tr>"; 

Print "<td>".$info['postCount'] . " </td>";

Print "<td>".$info['UID'] . " </td>";



Print "<td>"."#"."</td>";



Print "<td>".$info['ID'] . "</td> "; 



Print "<td>".$info['Allowed'] . "</td> ";



Print "<td>".$info['pass'] . " </td>";



Print "<td>".$info['HR'] . " </td>"; } 

?>

Link to comment
https://forums.phpfreaks.com/topic/215513-select-count/#findComment-1120668
Share on other sites

sorry, mixed up a with y.

 

$data = mysql_query("SELECT y.*, (SELECT COUNT(posts) FROM YBK_Ads a WHERE a.UID = y.UID) AS postCount FROM YBK_Login y") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { Print "<tr>"; Print "<td>".$info['postCount'] . " </td>";Print "<td>".$info['UID'] . " </td>";Print "<td>"."#"."</td>";Print "<td>".$info['ID'] . "</td> "; Print "<td>".$info['Allowed'] . "</td> ";Print "<td>".$info['pass'] . " </td>";Print "<td>".$info['HR'] . " </td>"; } ?>

 

Link to comment
https://forums.phpfreaks.com/topic/215513-select-count/#findComment-1120723
Share on other sites

@eran works perfectly Thanks both of you :)

$data = mysql_query("SELECT y.*,COUNT(ads.UID) AS posts FROM YBK_Login AS y LEFT JOIN YBK_Ads AS ads ON ads.UID=y.ID GROUP BY y.ID") or die(mysql_error()); 
while($info = mysql_fetch_array( $data )) {  
Print "<tr>"; 
Print "<td>".$info['UID'] . " </td>";
Print "<td>".$info['posts'] . " </td>";
Print "<td>".$info['ID'] . "</td> "; 
Print "<td>".$info['Allowed'] . "</td> ";
Print "<td>".$info['pass'] . " </td>";
Print "<td>".$info['HR'] . " </td>"; } 

 

Link to comment
https://forums.phpfreaks.com/topic/215513-select-count/#findComment-1120831
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.