View Full Version : will there be results in mysql_query?

05-07-2004, 09:55 PM
$link = mysql_connect ($Host, $User, $Password);
mysql_select_db($DBName, $link);

$Query = "SELECT * FROM $_GET[$Cat]";
$Result = mysql_query ($Query);

Will there be a $Result if that php scripting section is done? Then I start another one IE:

while ($Row = mysql_fetch_array($Result)) {
print ("<ul><li>$Row[Category]</li>");
mysql_close ($Link);
print ("</ul>");

05-07-2004, 10:12 PM
Will there be a $Result if that php scripting section is done? Then I start another one IE:
Well, how would we know ?

If the rest of your code is correct (so if all these variables are set correctly) and if there are records inside tat table, the $Result will contain a resourceID.

Your <ul> tag should be outside the loop, by the way

05-08-2004, 04:00 AM
you can check with mysql_num_rows() , the '@' supresses error messages

if( @mysql_num_rows( $Result ) ){
echo '<ul>' ;
while ( $Row = mysql_fetch_assoc( $Result ) ) {
echo "<li>{$Row['Category']}</li>" ;
echo '</ul>' ;
echo 'Query result was empty' ;

05-09-2004, 08:14 PM
mysql_num_rows ALWAYS returns TRUE.

if there are no results, mysql_num_rows will return 0, not FALSE.

so using if(mysql_num_rows($result)){ } will always evaluate as true, unless the query failed (if the query did fail it would output an error message anyway...)

if(mysql_num_rows($result) > 0){ } on the other hand, will only return true if there is at least 1 row in the result.

05-09-2004, 08:38 PM
You've chose a nickname that realy fits you.

and you're prooving you:
- don't know much about PHP and how it's conditional evaluations work, because 0 is often evaluated as False
- did not tried the code that was given to you by a far more experience coder then you are.

So it might be a good idea to get your facts straight before starting to lecture here.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum