简体   繁体   English

验证SQL查询是否没有返回结果

[英]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. $ data是查询的结果,我希望PHP在没有返回结果的情况下实现并显示回显“没有匹配的结果...”,如果DB确实返回了结果,则回显该表。 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. 使用mysql_num_rows确定查询返回的结果数。 If it is 0 there are no results. 如果为0 ,则没有结果。

But in general you should stop using the old legacy mysql_ methods. 但是通常,您应该停止使用旧的旧版mysql_方法。 They are marked as deprecated . 它们被标记为已弃用 You could use PDO or mysqli 您可以使用PDOmysqli

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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