php无法使用Empty()检查PDO结果是否为空,返回致命错误 [英] php cannot check if a PDO result is empty using empty() returns FATAL ERROR

查看:79
本文介绍了php无法使用Empty()检查PDO结果是否为空,返回致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

致命错误:无法在写入上下文中使用方法返回值

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

Whether I use PDO->fetchALL or PDO->fetch I receive the same error. Should I be doing something differently?

推荐答案

您需要将结果分配给变量,然后对该变量调用 empty().这只是 empty()函数的一个令人讨厌的限制.参见问题.

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 this question.

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

这篇关于php无法使用Empty()检查PDO结果是否为空,返回致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆