如何检查MySQL结果是否在PHP中返回空? [英] How to check if MySQL results returned empty in PHP?
本文介绍了如何检查MySQL结果是否在PHP中返回空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查MySQL结果是否为空.如果MySQL查询结果为空,则不应执行条件. 如果MySQL在那产生数据&在其他情况下,我的错误我的消息在那里,但未显示任何错误消息. 我尝试了以下代码,但未在屏幕上显示任何警报或回显消息.
How to check MySQL results are empty or not. If MySQL query results are empty then else condition should not be executed. In case MySQL results in data there & in else condition my error my message is there but it is not showing any error message. I have tried the following code but not showing any alert or echo message on the screen.
<?php
$sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
$res = mysql_query($sql);
if (!empty($res)) {
while ($row = mysql_fetch_row($res)) {
// here my data
}
} else {
echo "no results found";
}
推荐答案
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "id: " . $row["id"] . " - Name: " . $row["firstname"] . " " . $row["lastname"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
这篇关于如何检查MySQL结果是否在PHP中返回空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文