...

View Full Version : will there be results in mysql_query?



DiarYofaMadmaN
05-07-2004, 10:55 PM
<?PHP
$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:


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

raf
05-07-2004, 11: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

firepages
05-08-2004, 05: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>' ;
}else{
echo 'Query result was empty' ;
}
?>

SmArTaSs
05-09-2004, 09: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.

raf
05-09-2004, 09: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