PDO函数setFetchMode [英] PDO Function setFetchMode

查看:353
本文介绍了PDO函数setFetchMode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我试图用PDO做功能,但是得到错误(新到PDO)这里是我的代码

 函数mail_id ($ mail){
global $ host,$ dbname,$ user,$ pass;
$ DBH =新的PDO(mysql:host = $ host; dbname = $ dbname,$ user,$ pass);
$ STH = $ DBH->查询(从邮件中选择count(from)from ='$ mail');
$ STH-> setFetchMode(PDO :: FETCH_ASSOC);
$ row = $ STH-> fetch();
返回$ row;

$ / code>

我想计算行FROM和这是输出我正在尝试

  $ mail = mail_id($ userid); 
if(0< count($ mail ['to_viewed'])){echogreen;} else {echogrey;}

这是错误

 调用成员函数setFetchMode()on一个非对象

请帮助thx

解决方案

您需要检查的返回值 PDO ::查询() 。在(丑陋的)PHP中,有很多函数返回混合类型的值。在 PDO :: query()的情况下,返回类型是 PDOStatement 或bool,尽管文档中的原型提供了不同的内容:


描述:

PDOStatement PDO :: query(string $ statement)

...


像它总是返回 PDOStatement


返回值:
PDO :: query()返回一个PDOStatement对象,或者在失败时返回FALSE。



<哎呀,不是每一种情况!因此,您不能保证返回的值是 PDOStatement



正确的原型是:


描述:
混合PDO :: query(string $声明)

...


在您的情况下,查询无效(通过使用保留关键字,如FROM作为列名),这会导致返回布尔型类型的值为FALSE的值。一个布尔值不是一个对象,因此你对 $ STH-> setFetchMode()的调用失败。

根据你的PDO :: ATTR_ERRMODE,你得到了一个异常(PDO :: ERRMODE_EXCEPTION),所以你不需要检查


  • b $ b返回值
  • 警告(PDO :: ERRMODE_WARNING)
  • nothing(PDO :: ERRMODE_SILENT),所以你必须检查返回值值, errorCode() errorInfo()


Hello i was trying to make function with PDO but getting error (new to PDO) here is my code

    function mail_id($mail){
    global $host, $dbname, $user, $pass;
    $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $STH = $DBH->query("select count(from) from messages where from = '$mail'");
    $STH->setFetchMode(PDO::FETCH_ASSOC);
    $row = $STH->fetch();
    return $row;
}

i want to count row FROM and here is the out put i was trying

$mail=mail_id($userid);
if (0<count($mail['to_viewed'])) {echo "green";} else {echo "gray";}

this is the error

Call to a member function setFetchMode() on a non-object

please help thx

解决方案

You need to check the return value of PDO::query(). In (ugly) PHP there are lots of functions that return a value of mixed type. In the case of PDO::query() the return type is either PDOStatement or bool, although the prototype in the documentation says something different:

Description:
PDOStatement PDO::query ( string $statement )
...

Looks like it always returns PDOStatement.

Return Values:
PDO::query() returns a PDOStatement object, or FALSE on failure.

Oops, not in every case! Therefore you are not guaranteed that the value returned is a PDOStatement.

The correct prototype would be:

Description:
mixed PDO::query ( string $statement )
...

In your case the query is invalid (by using a reserved keyword like FROM as a column name) which results in a return value of type boolean with the value FALSE. A boolean value is not an object and therefore your call to $STH->setFetchMode() fails.

Depending on your PDO::ATTR_ERRMODE you get

  • an exception (PDO::ERRMODE_EXCEPTION), so you don't need to check the return value
  • a warning (PDO::ERRMODE_WARNING)
  • nothing (PDO::ERRMODE_SILENT), so you have to check the return value, errorCode() and errorInfo()

这篇关于PDO函数setFetchMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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