gpt4 book ai didi

php - 如何检测mySQL中的表行是否有文件?

转载 作者:行者123 更新时间:2023-11-29 07:29:26 25 4
gpt4 key购买 nike

我的 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";
};

但现在我遇到了问题,我得到 id 12: there is content 和 id 3:没有内容。但对于 name2 来说,实际上应该始终是没有内容。我做错了什么?

最佳答案

如果您的列为空(不是 null,而是 '' )

if(!result) 

将被评估为 false。

尝试:

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

关于php - 如何检测mySQL中的表行是否有文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33211254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com