简体   繁体   中英

Validate if there are no results returned from SQL query

below is my code. $data is the result of the query and i want the PHP to realise when there are no results returned and display the echo "There were no matching results..." if the DB does return a result, echo the table. the following doesn't realise when there are no results returned...

if ($data == null) {

        echo "<strong>There were no Matching Results...Please Return to the '<a href='section_search.php'>Section Search Page</a>'</strong></br></br></br>";

     }

     else {
      echo "<table border=0 cellpadding=10>";
    echo "<tr align = center bgcolor=white>
    <td><b>Company</b></td><td><b>Section</b></td><td><b>Question</b></td><td><b>Answer</b></td>" ; 
     while($info = mysql_fetch_array( $data )) 
     { 
     echo "<tr>"; 
     echo "<td width = 60px><b>".boldText($info['company_name'], $kword) . "</b></td> ";
     echo "<td width = 60px><b>".boldText($info['section_name'], $kword) . "</b></td> "; 
     echo "<td width = 360px>".boldText($info['question'], $kword) . " </td>";
     echo "<td width = 600px>".boldText($info['answer'], $kword) . " </td></tr>"; 
     } 
     echo "</table>"; 
     }
    }

any help would be great!

Thanks

Use mysql_num_rows to determine the number of results returned by your query. If it is 0 there are no results.

But in general you should stop using the old legacy mysql_ methods. They are marked as deprecated . You could use PDO or mysqli

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM