如何检查mysql结果是否为空。如果mysql查询结果为空,则不应该删除condtion。 如果mysql结果数据那里& in else条件我的错误我的消息在那里,但它没有显示任何错误消息。 我尝试了以下代码,但没有在屏幕上显示任何警报或回显消息。
<?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";
echo "<br>";
echo "<script>alert('No any result found..!!');</script>";
echo "no results found";
}
?>
&#13;
答案 0 :(得分:7)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$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();
?>
&#13;
答案 1 :(得分:1)
检查行数
$result = mysqli_query($conn, $sql);
$rowcount=mysqli_num_rows($result);
if($rowcount > 0){
echo "Number of rows = " . $rowcount;
}
else
{
echo "no record found";
}
答案 2 :(得分:1)
安全提示:首先停止使用
mysql_*
函数,因为它们对生产不安全,并且更高版本已停止对此API的支持。因此,如果不小心在生产中使用了这些功能,则可能会遇到麻烦。不建议将旧的mysql扩展用于新开发,因为它在PHP 5.5.0中已弃用,在PHP 7中已删除。下面提供了详细的功能比较矩阵。 More Read
对于您的答案,您只需检查no of rows
是否为零
通过示例阅读php documentation处的帖子。
答案 3 :(得分:0)
您可以使用mysql_num_rows检查结果集的编号:
$sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
$res = mysql_query($sql);
if(isset($res))
{
$noRows = mysql_num_rows($res);
if($noRows > 0){
while($row=mysql_fetch_row($res))
{
here my data
}
}else{
echo "no results found";
echo "<br>";
echo "<script>alert('No any result found..!!');</script>";
echo "no results found";
}
}
在这里找到mysql_num_rows的文档:
答案 4 :(得分:0)
您可以使用mysql_num_rows();
检查查询返回行是否
$sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
$res = mysql_query($sql);
$rows=mysql_num_rows($res);
if($rows>0)
{
echo "data return from query";
}else{
echo "data not return";
}
注意: - 不推荐使用mysql而是使用mysqli或PDO
答案 5 :(得分:0)
您可以使用mysql_num_rows来获取查询返回的行数。
if(mysqli_num_rows($res) > 0)
{
// rest of your stuff
}
else
{
echo "No records found.";
}
答案 6 :(得分:0)
你可以用这样的mysql_num_rows替换if(!empty($res))
-
$sql = "select * from hall_search_data_1 where rent BETWEEN '".$_SESSION['amount1']."' AND '".$_SESSION['amount2']."'";
$res = mysql_query($sql);
if($countrows = mysql_num_rows($res) >= 1){
while($row=mysql_fetch_row($res))
{
here my data
}
}else{
echo "no results found";
echo "<br>";
echo "<script>alert('No any result found..!!');</script>";
echo "no results found";
}
答案 7 :(得分:0)
mysql_*
API早已从PHP中删除。要访问数据库,您应该使用PDO。检查PDO是否返回任何结果实际上非常简单。 仅获取结果,如果数组为空,则MySQL没有任何返回。
$stmt = $pdo->prepare('SELECT * FROM hall_search_data_1 WHERE rent BETWEEN ? AND ?');
$stmt->execute([$_SESSION['amount1'], $_SESSION['amount2']]);
$records = $stmt->fetchAll();
if ($records) {
foreach ($records as $row) {
// your logic
}
} else {
echo 'No records found!';
}
还有mysqli库,如果您坚持使用它,则必须做更多的工作,但是想法是相同的。获取所有结果,如果没有获取任何内容,则意味着MySQL不返回任何行。
$stmt = $mysqli->prepare('SELECT * FROM hall_search_data_1 WHERE rent BETWEEN ? AND ?');
$stmt->bind_param('ss', $_SESSION['amount1'], $_SESSION['amount2']);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
if ($records) {
foreach ($records as $row) {
// your logic
}
} else {
echo 'No records found!';
}