处理错误报告

时间:2013-07-26 11:11:41

标签: php

我是PHP的新手,所以我不得不为这样一个愚蠢的问题道歉,但我不知道如何找到正确的答案。我应该检查我的最终结果是否为空(如果$ sql发现了什么)。如果它没有找到我想得到一些通知示例“列表是空的”。当我调用网址时,该应用也会显示该消息?

<?php
$host = ""; 
$user = ""; 
$pwd = ""; 
$db = ""; 

$con = mysqli_connect($host, $user, $pwd, $db);

if(mysqli_connect_errno($con)) {
    die("Failed to connect to MySQL: " . mysqli_connect_error());
} 

// query the application data

$sql = "SELECT * FROM lista WHERE Grad='".$_GET['grad']."' AND Predmet='".$_GET['predmet']."'";

$result = mysqli_query($con, $sql);

$rows = array();

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {


        $rows[] = $row;     
}

mysqli_close($con);

echo json_encode($rows);

1 个答案:

答案 0 :(得分:1)

如果mysqli_num_rows返回0,则表示没有记录。

$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) == 0) {
    $rows = "no rows found";
} else {
    $rows = array();
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $rows[] = $row;     
    }
}
mysqli_close($con);
echo json_encode($rows);