WatsonN Posted October 9, 2010 Share Posted October 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/215513-select-count/ Share on other sites More sharing options...
joel24 Posted October 9, 2010 Share Posted October 9, 2010 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/215513-select-count/#findComment-1120668 Share on other sites More sharing options...
eran Posted October 9, 2010 Share Posted October 9, 2010 Left join the ads table 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 Quote Link to comment https://forums.phpfreaks.com/topic/215513-select-count/#findComment-1120681 Share on other sites More sharing options...
WatsonN Posted October 10, 2010 Author Share Posted October 10, 2010 @joel24 I got unknown table a @eran it gives be a blank value it would be $info['postCount'] right? Quote Link to comment https://forums.phpfreaks.com/topic/215513-select-count/#findComment-1120709 Share on other sites More sharing options...
joel24 Posted October 10, 2010 Share Posted October 10, 2010 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/215513-select-count/#findComment-1120723 Share on other sites More sharing options...
eran Posted October 10, 2010 Share Posted October 10, 2010 @joel24 I got unknown table a @eran it gives be a blank value it would be $info['postCount'] right? No, it should be $info['posts']. var_dump($info) to see what it contains Quote Link to comment https://forums.phpfreaks.com/topic/215513-select-count/#findComment-1120766 Share on other sites More sharing options...
WatsonN Posted October 10, 2010 Author Share Posted October 10, 2010 @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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/215513-select-count/#findComment-1120831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.