检查mysqli_query是否为空

时间:2018-04-30 17:04:31

标签: php mysql

我正在寻找一种方法来检查我的mysqli_query结果是否为空。

我的代码:

$result = mysqli_query($con, "SELECT ......");

然后是桌头:

<table class="table table-striped" width="100%">
<thead>
<table class="table table-striped" width="100%">
        <thead>
            <th>Jahr</th>

然后我使用while循环生成行:

while($ausgabe = mysqli_fetch_object($result)){ ?>
        <tr>
            <td><?php echo $ausgabe->jahr; ?></td>

现在我想创建一个if查询来检查是否必须在表开始之前显示表,如下所示:

if($result){    
<table class="table table-striped" width="100%">
        <thead>
            <th>Jahr</th>

但是这不起作用,它只适用于询问mysqli_fetch_object是否为空但这段代码在我的桌面下面,所以我不能使用它。

你有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您可以使用$num_rows

$results = $mysqli->query("SELECT * FROM table");
if($results->num_rows === 0)
{
   echo 'No results';
 }
else
{
//HERE YOU PROCESS 
while(){....}
}