PHP & MySql 检查表是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4854148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:05:43  来源:igfitidea点击:

PHP & MySql check if table is empty

phpmysqlif-statement

提问by user517593

I'm a bit of a noob- and I'm having a hard time...

我有点菜鸟 - 我很难过......

I need a bit of of code that searches a db table to find the row that matches the $id variable. There's a field in that table 'description' that I need to grab. If it's null, I need to show one message, if not another. Here's the code I have (I know I need to add the mysqli escape string, just doing this real quick from memory):

我需要一些代码来搜索 db 表以找到与 $id 变量匹配的行。我需要获取该表“描述”中的一个字段。如果它为空,我需要显示一条消息,如果不是另一条。这是我的代码(我知道我需要添加 mysqli 转义字符串,只需从内存中快速执行此操作):

$query = "SELECT description FROM posts WHERE id = $id";
$result = mysqli_query($dbc, $query);

$row = mysqli_fetch_array($result, MYSQLI_ASSOC) ;

if(!$row){
echo "<p>'No description'</p>";
} else {
echo '<p>' . $row['description'] . '</p>';
}

回答by Josh

mysqli_fetch_arraywill fetch a row regardless of if the columns in that row are null. You want to be checking if $row['description']is set instead of if $rowis set:

mysqli_fetch_array无论该行中的列是否为空,都将获取一行。您想检查是否$row['description']已设置而不是是否$row已设置:

$query = "SELECT description FROM posts WHERE id = $id";
$result = mysqli_query($dbc, $query);

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

if(isset($row['description'])) {
    echo "<p>No description</p>";
} else {
    echo '<p>' . $row['description'] . '</p>';
}

EDIT:Or, as an alternative, you can not fetch rows from the database where description is NULL:

编辑:或者,作为替代方案,您无法从描述为 NULL 的数据库中获取行:

$query = "SELECT description FROM posts WHERE id = $id AND description IS NOT NULL LIMIT 1";
$result = mysqli_query($dbc, $query);

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

if(! $row) {
    echo "<p>No description</p>";
} else {
    echo '<p>' . $row['description'] . '</p>';
}

Nowyou'd check to see if you were able to grab a row or not.

现在你要检查你是否能够抓住一行。

回答by Marc

The !$rowwill only occur if no record is found. If the field description is really null, you have to check it this way:

!$row如果没有记录被发现时才会发生。如果字段描述真的是null,你必须这样检查:

if(is_null($row['description'])){

but I recommend you to check if the value is empty (or 0 or null):

但我建议您检查该值是否为空(或 0 或 null):

if(empty($row['description'])){

回答by Sarfraz

BTW, you can do the check from within your query using COALESCE:

顺便说一句,您可以使用COALESCE以下方法从查询中进行检查:

$query = "SELECT COALESCE(description, 'No description') FROM posts WHERE id = $id";
$result = mysqli_query($dbc, $query);

$row = mysqli_fetch_array($result, MYSQLI_ASSOC) ;
echo $row['description'];

This way, when there is a value for the descriptionfield, it will be shown otherwise No descriptionwill be output. So that way, you can do away with the ifcondition of PHP.

这样,当该description字段有值时,它将被显示,否则No description将被输出。这样,您就可以摆脱ifPHP的条件。

回答by Michael B

How about:

怎么样:

SELECT COUNT(*) FROM table WHERE `description` IS NOT NULL