blurredvision Posted August 3, 2008 Share Posted August 3, 2008 I'm familiar with mysqli_fetch_array when returning values and such, but for the first time, I'm needing to use the COUNT(*) function in mysql. Here's my code: $winsquery1 = "SELECT COUNT(*) FROM hometeam_stats WHERE gamertag_id='{$standings['gamertag_id']}' AND season='$currentseason' AND win_loss=1"; $runqueries1 = @mysqli_query($dbc, $winsquery1); Usually, I would just set up $runqueries1 with the fetch array and go from there. But how do I simply extract the number that my query should hopefully be getting? Quote Link to comment https://forums.phpfreaks.com/topic/117945-solved-how-to-return-the-value-of-a-mysql-count-query-in-php/ Share on other sites More sharing options...
moselkady Posted August 3, 2008 Share Posted August 3, 2008 You will still use mysql_fetch_array as you usually do except that you will use an index number instead of a field name <?php $result = mysql_fetch_array ($runqueries1); $count = $result[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/117945-solved-how-to-return-the-value-of-a-mysql-count-query-in-php/#findComment-606713 Share on other sites More sharing options...
wildteen88 Posted August 3, 2008 Share Posted August 3, 2008 You will still use mysql_fetch_array as you usually do except that you will use an index number instead of a field name <?php $result = mysql_fetch_array ($runqueries1); $count = $result[0]; ?> Not necessarily you can do $result['COUNT(*)'] Howwer a better option would be give the count a name in your query, eg $winsquery1 = "SELECT COUNT(*) as stats_count FROM hometeam_stats WHERE gamertag_id='{$standings['gamertag_id']}' AND season='$currentseason' AND win_loss=1"; And use $result['stats_count'] instead. Quote Link to comment https://forums.phpfreaks.com/topic/117945-solved-how-to-return-the-value-of-a-mysql-count-query-in-php/#findComment-606733 Share on other sites More sharing options...
Nhoj Posted August 3, 2008 Share Posted August 3, 2008 You could also just do it all in one query with mysql_result like so: <? $count = mysql_result(mysql_query("SELECT COUNT(*) FROM hometeam_stats WHERE gamertag_id='{$standings['gamertag_id']}' AND season='$currentseason' AND win_loss=1"), 0); echo $count; ?> Quote Link to comment https://forums.phpfreaks.com/topic/117945-solved-how-to-return-the-value-of-a-mysql-count-query-in-php/#findComment-606735 Share on other sites More sharing options...
PFMaBiSmAd Posted August 3, 2008 Share Posted August 3, 2008 Please don't get in the habit of nesting several functions, especially if the innermost can fail and return a FALSE value instead of the expected results as this prevents error checking, error reporting, error logging, and error recovery logic that would be present in a real application. Quote Link to comment https://forums.phpfreaks.com/topic/117945-solved-how-to-return-the-value-of-a-mysql-count-query-in-php/#findComment-606743 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.