php pdo query 空判断,检查空结果(php,pdo,mysql)

检查空结果(php,pdo,mysql)

拜托,有人可以告诉我我在做什么错吗? 我只是从表中检索结果,然后将它们添加到数组中。 一切正常,直到我检查为空结果为止。

这将得到匹配,将其添加到我的数组中,并按预期回显结果:

$today = date('Y-m-d', strtotime('now'));

$sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");

$sth->bindParam(':today',$today, PDO::PARAM_STR);

if(!$sth->execute()) {

$db = null ;

exit();

}

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

$this->id_email[] = $row['id_email'] ;

echo $row['id_email'] ;

}

$db = null ;

return true ;

当我尝试检查一个空结果时,我的代码返回“空”,但不再产生匹配结果:

$today = date('Y-m-d', strtotime('now'));

$sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");

$sth->bindParam(':today',$today, PDO::PARAM_STR);

if(!$sth->execute()) {

$db = null ;

exit();

}

if ($sth->fetchColumn()) {

echo 'not empty';

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

$this->id_email[] = $row['id_email'] ;

echo $row['id_email'] ;

}

$db = null ;

return true ;

}

echo 'empty';

$db = null ;

return false ;

与往常一样,我们将提供任何帮助。 谢谢!

canoebrain asked 2020-06-24T09:20:29Z

7个解决方案

87 votes

当您执行$sth->fetchColumn()时,将丢弃结果行。这不是检查是否有任何结果的方式。 你做

if ($sth->rowCount() > 0) {

... got results ...

} else {

echo 'nothing';

}

相关文档在这里:[http://php.net/manual/en/pdostatement.rowcount.php]

Marc B answered 2020-06-24T09:20:54Z

10 votes

如果您可以选择使用fetchAll(),则如果没有返回行,则将为空数组。

count($sql->fetchAll(PDO::FETCH_ASSOC))

将返回返回的行数。

josh123a123 answered 2020-06-24T09:21:19Z

8 votes

即使这是一个老话题,我还是认为我会加入进来,因为最近我不得不处理这个问题。

您不应将rowCount用于SELECT语句,因为它不可移植。 我使用isset函数测试select语句是否起作用:

$today = date('Y-m-d', strtotime('now'));

$sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");

//I would usually put this all in a try/catch block, but kept it the same for continuity

if(!$sth->execute(array(':today'=>$today)))

{

$db = null ;

exit();

}

$result = $sth->fetch(PDO::FETCH_OBJ)

if(!isset($result->id_email))

{

echo "empty";

}

else

{

echo "not empty, value is $result->id_email";

}

$db = null;

当然,这仅适用于单个结果,就像您遍历数据集时那样。

stubsthewizard answered 2020-06-24T09:21:48Z

8 votes

$sql = $dbh->prepare("SELECT * from member WHERE member_email = '$username' AND member_password = '$password'");

$sql->execute();

$fetch = $sql->fetch(PDO::FETCH_ASSOC);

// if not empty result

if (is_array($fetch)) {

$_SESSION["userMember"] = $fetch["username"];

$_SESSION["password"] = $fetch["password"];

echo 'yes this member is registered';

}else {

echo 'empty result!';

}

Mahmoud Ali answered 2020-06-24T09:22:03Z

3 votes

我在这里做错了什么?

几乎所有的东西。

$today = date('Y-m-d'); // no need for strtotime

$sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");

$sth->bindParam(':today',$today); // no need for PDO::PARAM_STR

$sth->execute(); // no need for if

$this->id_email = $sth->fetchAll(PDO::FETCH_COLUMN); // no need for while

return count($this->id_email); // no need for the everything else

有效地,您始终可以获取数据(在本例中为$this->id_email变量)来判断您的查询是否返回了任何内容。 在我有关PDO的文章中阅读更多内容。

Your Common Sense answered 2020-06-24T09:22:32Z

1 votes

要考虑的另一种方法:

在构建HTML表或其他依赖于数据库的内容时(通常通过AJAX调用),我想在进行任何标记之前检查SELECT查询是否返回了任何数据。 如果没有数据,我只返回“找不到数据...”或类似的信息。 如果有数据,请继续进行操作,构建标头并遍历内容,依此类推。尽管我可能将数据库限制为MySQL,但我更喜欢编写可移植的代码,因此rowCount()消失了。 而是,检查列数。 不返回任何行的查询也不会返回任何列。

$stmt->execute();

$cols = $stmt->columnCount(); // no columns == no result set

if ($cols > 0) {

// non-repetitive markup code here

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

lango answered 2020-06-24T09:22:57Z

0 votes

多亏了Marc B的帮助,这才对我有用(请注意:Marc的rowCount()建议也可以工作,但是我对它不能在其他DB上工作或者我的内容有所更改的可能性感到不满意... ,他的选择count(*)建议也可以使用,但是,我认为,因为如果仍然存在数据,我最终将获得数据,因此我采用了这种方式)

$today = date('Y-m-d', strtotime('now'));

$sth = $db->prepare("SELECT id_email FROM db WHERE hardcopy = '1' AND hardcopy_date <= :today AND hardcopy_sent = '0' ORDER BY id_email ASC");

$sth->bindParam(':today',$today, PDO::PARAM_STR);

if(!$sth->execute()) {

$db = null ;

exit();

}

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {

$this->id_email[] = $row['id_email'] ;

echo $row['id_email'] ;

}

$db = null ;

if (count($this->id_email) > 0) {

echo 'not empty';

return true ;

}

echo 'empty';

return false ;

canoebrain answered 2020-06-24T09:23:18Z

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值