php 无法使用 empty() 返回 FATAL ERROR 检查 PDO 结果是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10677438/
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-24 22:47:17  来源:igfitidea点击:

php cannot check if a PDO result is empty using empty() returns FATAL ERROR

phppdofatal-error

提问by Brook Julias

I want to check whether my prepared query has returned empty or not without having to go into a loop. This is the code I have tried using:

我想检查我准备好的查询是否返回空,而不必进入循环。这是我尝试使用的代码:

if(empty($pQuery1->fetch(PDO::FETCH_ASSOC))){}

When I try this I get the error:

当我尝试这个时,我收到错误:

Fatal error: Can't use method return value in write context

Fatal error: Can't use method return value in write context

Whether I use PDO->fetchALLor PDO->fetchI receive the same error. Should I be doing something differently?

无论我使用PDO->fetchALL还是PDO->fetch收到相同的错误。我应该做些不同的事情吗?

回答by James Skidmore

You need to assign the results to a variable, then call empty()on the variable. It's just an annoying limitation of the empty()function. See thisquestion.

您需要将结果分配给一个变量,然后调用empty()该变量。这只是功能的一个恼人的限制empty()。看到这个问题。

$results = $pQuery1->fetch(PDO::FETCH_ASSOC);
if (empty($results)){}