SelfObscurity Posted November 30, 2009 Share Posted November 30, 2009 Morning, I'm working on some scripting for an online gaming web site. I'm trying to display wins, losses and ties based on what type of game was played. That is working just fine. But I'm trying to set it up so when it counts the number of rows where the result field = Wins, if the row count is 0, i want to display 0. Currently it is blank. Below is my code that is not working. I've been working on this for over a day now, and I can't seem to get it to work. Can anyone help? <?php $result = mysql_query("SELECT * FROM matches, leagues WHERE matches.type = leagues.nameshort AND result=\"Win\" GROUP BY result"); $result = mysql_fetch_row($result); if (mysql_num_rows($result)>0) { echo $result; } else { echo "0"; } ?> With this code, the error I receive is this: Warning: mysql_num_rows() expects parameter 1 to be resource, array given in C:\xampp\htdocs\projects\cso\index.php on line 99 Line 99 is the if statement. Quote Link to comment https://forums.phpfreaks.com/topic/183433-if-row-count-0-problems/ Share on other sites More sharing options...
premiso Posted November 30, 2009 Share Posted November 30, 2009 You need to encapsulate string variables inside a sql in Single quotes, not double. $result = mysql_query("SELECT * FROM matches, leagues WHERE matches.type = leagues.nameshort AND result='Win' GROUP BY result"); As well if you would of had this for testing you would have known the error: $result = mysql_query("SELECT * FROM matches, leagues WHERE matches.type = leagues.nameshort AND result='Win' GROUP BY result") or die(mysql_error()); EDIT: As a note the or DIE() should be loosely used and perhaps you should change your script to incorporate error handling via this debugging tutorial look into the "trigger_error" portion. Quote Link to comment https://forums.phpfreaks.com/topic/183433-if-row-count-0-problems/#findComment-968219 Share on other sites More sharing options...
rajivgonsalves Posted November 30, 2009 Share Posted November 30, 2009 your overwriting $result with mysql_fetch_row, $result should be the resource identifier of your mysql_query the first one Quote Link to comment https://forums.phpfreaks.com/topic/183433-if-row-count-0-problems/#findComment-968220 Share on other sites More sharing options...
SelfObscurity Posted November 30, 2009 Author Share Posted November 30, 2009 I didn't catch that. I recently changed how I was doing it to this, overlooked it. I changed it and am still receiving an error. <?php $result = mysql_query('SELECT * FROM matches, leagues WHERE matches.type = leagues.nameshort AND result=\"Win\" GROUP BY result'); $count = mysql_fetch_row($result); if (mysql_num_rows($result)>0) { echo $count; } else { echo "0"; } ?> Error: Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\projects\cso\index.php on line 98 Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\projects\cso\index.php on line 99 Quote Link to comment https://forums.phpfreaks.com/topic/183433-if-row-count-0-problems/#findComment-968230 Share on other sites More sharing options...
premiso Posted November 30, 2009 Share Posted November 30, 2009 You need to encapsulate string variables inside a sql in Single quotes, not double. $result = mysql_query("SELECT * FROM matches, leagues WHERE matches.type = leagues.nameshort AND result='Win' GROUP BY result"); I posted the correct code there...try that and see if it helps you out. Cause your SQL would still have been throwing an error due to the double quotes after result="win" it should be result='Win' (as shown correctly above). Quote Link to comment https://forums.phpfreaks.com/topic/183433-if-row-count-0-problems/#findComment-968233 Share on other sites More sharing options...
SelfObscurity Posted November 30, 2009 Author Share Posted November 30, 2009 I mis-read your post, I thought you were talking about the entire string beginning with SELECT...it made no sense to me so I didn't look at the full code you posted! Now, I've never seen this before and forgive me if it's a simple fix. I corrected the " to a ' and now where it should say 1 (because I have 1 row with a result=Win) it says Array ? Quote Link to comment https://forums.phpfreaks.com/topic/183433-if-row-count-0-problems/#findComment-968238 Share on other sites More sharing options...
premiso Posted November 30, 2009 Share Posted November 30, 2009 <?php $result = mysql_query("SELECT * FROM matches, leagues WHERE matches.type = leagues.nameshort AND result='Win' GROUP BY result"); $row = mysql_fetch_row($result); // this fetches the data as an array ordered by a numbered index. $rowCount = mysql_num_rows($result); // this will give us the number of rows returned if ($rowCount>0) { echo $rowCount; }else { echo "0"; } ?> Put my remarks in the comments of the code. Quote Link to comment https://forums.phpfreaks.com/topic/183433-if-row-count-0-problems/#findComment-968239 Share on other sites More sharing options...
SelfObscurity Posted November 30, 2009 Author Share Posted November 30, 2009 Premiso, Thank you for your help. The comments helped greatly. Quote Link to comment https://forums.phpfreaks.com/topic/183433-if-row-count-0-problems/#findComment-968255 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.