如何检测mySQL中的表行是否包含文件?

时间:2015-10-19 09:43:15

标签: php mysql

我的SQL数据库中有一个表friends

╔════╦═══════╦═══════╦═══════╗
║ id ║ name1 ║ name2 ║ name3 ║
╠════╬═══════╬═══════╬═══════╣
║ 1  ║ fred  ║       ║       ║
║ 2  ║ tom   ║       ║       ║
╚════╩═══════╩═══════╩═══════╝

现在我想检测name2是否有任何内容。

$sql = $pdo->prepare("SELECT name2 FROM friends WHERE id = '$id'");
                            $sql->execute();
                            $result = $sql->fetch(PDO::FETCH_ASSOC);
                            if (!$result) {
                            echo "there is no content";
                            } else {
                            echo "there is content";
                            };

但是现在我遇到了问题,我得到了身份12there is content以及身份3there is no content。但对于name2,实际上应该总是there is no content。我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果你的列是空的(不是空,而是'')

if(!result) 

将被评估为假。

尝试:

if(!result || result == '')