...

View Full Version : displaying a statement for 'no results'



xAtlas
06-15-2006, 09:35 PM
Hi. I'm pretty new to the php coding thing, kinda learning as I go when I have time. Well, I getting ready to start up a gaming ladder and when I click on our "match results" link, it outputs this error:



Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /*/*/*/bani/functions/matchdb.php on line 61

And thats because the database is empty as there are no recorded matches yet. I tested this. If I entered in a match, the error goes away and the match result is displayed.

Here's the code:


$result=mysql_query("SELECT matchid,winnerid,loserid,winneralias,loseralias,ladderid,laddername,DATE_FORMAT(reportdate, '%M %d, %l:%i %p'),reporterip,confirmerip from matchdb ORDER BY reportdate desc LIMIT $start,$stop");

while(list($matchid,$winnerid,$loserid,$winneralias,$loseralias,$ladderid,$laddername,$reportdate,$r eporterip,$confirmerip)=mysql_fetch_row($result))


Note: Line 61 is the "while(list"

I can't figure out how to make it so that when it searches the db and there isn't anything, it will say "There are no recorded matches at this time."

Can anyone help me? Thanks in advance.

Mwnciau
06-15-2006, 11:32 PM
when you put
$rows = mysql_fetch_rows() put an @ infront of it to hide the error and then in your main part put [PHP]if($rows >= 1){
echo 'The amount of rows is ' . $rows;
}
else {
echo 'There are no recorded matches at this time.';
}

Something like that will work...

Kid Charming
06-16-2006, 12:05 AM
Your error is not caused by an empty result set; there's something going wrong with your query call. Use mysql_error() (http://us2.php.net/manual/en/function.mysql-error.php) to find out what it is.

xAtlas
06-16-2006, 04:16 AM
Your error is not caused by an empty result set; there's something going wrong with your query call. Use mysql_error() (http://us2.php.net/manual/en/function.mysql-error.php) to find out what it is.

Ok. I did that and here is what it said:



A fatal MySQL error occured.
Query:
Error: (1065) Query was empty


Mwnciau: I'll try what you suggested. I don't need the output though for the for if there is something, just something to say there aren't any results. Thanks. I'll reply with results :).

_____

edit:

Didn't work. I still get the error. I also tried having the '@' in front of 'while' and I got a T_WHILE parse error. Just a note, I'm having a problem with the 'while(list(' statement... Still working on it. I tried to do:


if($row ==0 ){
echo 'There are no match records at this time.';
}

but that didn't work either.

Kid Charming
06-16-2006, 04:53 AM
Move the closing doublequotes outside the closing parenthesis in your query:

LIMIT $start,$stop)";

[Edit]My bad. Never mind this post.

xAtlas
06-16-2006, 05:04 AM
Move the closing doublequotes outside the closing parenthesis in your query:

LIMIT $start,$stop)";

Tried and got an ugly parse error. You're having me edit this line:


$result=mysql_query("SELECT


But the error says its being generated from this one:



while(list($matchid,$winnerid,$loserid,$winneralias,$loseralias,$ladderid,$laddername,$reportdate,$r eporterip,$confirmerip)=mysql_fetch_row($result));


It has the "=mysql_fetch_row($result)" at the end of it.

Kid Charming
06-16-2006, 05:09 AM
Yeah, I was high -- forget that last post of mine. :o


Try putting your query into a variable, echoing it out, and posting the result.

Kid Charming
06-16-2006, 05:11 AM
The mysql_num_rows() is raising the error, but that's because there's something wrong with your query -- your mysql_query() is returning FALSE instead of a result resource, and that's causing the mysql_num_rows() error.

xAtlas
06-16-2006, 05:20 AM
The mysql_num_rows() is raising the error, but that's because there's something wrong with your query -- your mysql_query() is returning FALSE instead of a result resource, and that's causing the mysql_num_rows() error.

oh. I see. Ok then, I'll see what I can draw up and post the results (again lol). thanks.

xAtlas
06-19-2006, 03:55 AM
Ok. After all this time, I still can't figure this one out. Can anyone out there throw me some other ideas?

____

EDIT:

I figured out what the problem was. It was this part of the 'mysql_query()':


desc LIMIT $start,$stop

Can anyone tell me what that is supposed to do? I'm modifying a ladder that was pre-scripted (which means the author did this).

flann
06-19-2006, 04:33 AM
try this were you are echo'ing out the query. You might see the problem right away, if not, run it in phpmyadmin or command shell, and see what it says.


$select = "SELECT matchid, winnerid, loserid, winneralias, loseralias, ladderid, laddername,
DATE_FORMAT(reportdate, '%M %d, %l:%i %p'), reporterip, confirmerip
FROM matchdb
ORDER BY reportdate desc
LIMIT $start,$stop";
$result=mysql_query($select) or die ("Can't get stuff " . mysql_error());
echo ($select);


do you get any errors?

flann
06-19-2006, 04:38 AM
to answer you question on what desc is doing. It is part of your ORDER BY, and it will order them descending. the LIMIT $start, $stop will start at a record set position and how many to rows from that position to display. An example of this would be say you have a record set that has 10 rows and each row has one field with letters a-j. LIMIT 1, 2 would display b and c. LIMIT 0,1 would display a.

Mwnciau
06-19-2006, 04:58 PM
have you fixed this yet?
If not try putting this around mysql_fetch_row() :



if ($results){// if the mysql_query returns info and there is no error
//...
}
else {
echo 'no rows!';
}

Kid Charming
06-19-2006, 05:09 PM
if ($results){// if the mysql_query returns info and there is no error




Just to clarify -- checking the boolean value of $results will only determine whether there has been an error in your mysql_query() call. It will not tell you if you have no data. If you send the query:



SELECT * FROM table WHERE column='foo'


and you don't have a column that equals 'foo', then you will still get a result resource. To check if you have rows, you then need to run that resource through mysql_num_rows(). mysql_query() only returns FALSE if there is an error sending the query.

xAtlas
06-19-2006, 07:48 PM
to answer you question on what desc is doing. It is part of your ORDER BY, and it will order them descending. the LIMIT $start, $stop will start at a record set position and how many to rows from that position to display. An example of this would be say you have a record set that has 10 rows and each row has one field with letters a-j. LIMIT 1, 2 would display b and c. LIMIT 0,1 would display a.

Ok. I understand what you are saying. But why is it causing the error? If I remove it, no error and everything is kosher. Could it be the $start,$stop? Should there be numbers instead? And thank you all that are helping :). I appreciate every instance!

Kid Charming
06-19-2006, 08:06 PM
$start and $stop are variables that should be populated with numbers. If you echo your query, you'll see exactly what is being send to the database and why it's failing. If $start and $stop aren't defined, for example, your query will end:

LIMIT ,

which will give you an error.

Setting your error reporting for this script to E_ALL will also tell you if your variables aren't defined.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum