Question

With the following PHP code, how do i check if there's no row retuned so i can echo some message?

PHP:

<?php 
    require_once 'db_conx.php';
    $Result = mysql_query("SELECT * FROM ads WHERE pid = '2'")
        or die (mysql_error());
    while($row = mysql_fetch_array($Result)){
        echo '<span class="classPid" style="display:none">'.$row['pid'].'</span>';
    }
?>

Thanks

Was it helpful?

Solution 2

Something like this

<?php 
    require_once 'db_conx.php';
    $Result = mysql_query("SELECT * FROM ads WHERE pid = '2'")
        or die (mysql_error());
    $i=0;
    while($row = mysql_fetch_array($Result)){
        echo "<span class='classPid' style='display:none'>".$row['pid']."</span>";
        $i++;
    }
    if($i==0){
        echo "No rows found";
    }
?>

OTHER TIPS

There are a number of ways to do this. There is a specific function mysql_num_rows() which will return the number of rows returned.

mysql_num_rows($Result);

This will return 0 if there are no rows affected or returned.

You could also create some conditions using mysql_fetch_array. mysql_fetch_array will return FALSE if there are no rows.

On a separate note it would be a good idea to update your connection and functions to mysqli as mysql is no depreciated.

See docs on mysql_num_rows().

http://www.php.net/mysql_num_rows

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top