Jump to content

If Row Count = 0 Problems


SelfObscurity

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

<?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.

Link to comment
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.