Home >Backend Development >PHP Tutorial >PDOStatement::setFetchMode()问题

PDOStatement::setFetchMode()问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:47:471158browse

<code>function fetch($query='', $type = 'BOTH') 
{
    $result_type="PDO::FETCH_".$type;
    $query->setFetchMode($result_type);//这报错,$query是pdo的query结果
    $result_arr=$query->fetch();
    return $result_arr;
}
</code>

PDOStatement::setFetchMode(): SQLSTATE[HY000]: General error: mode must be an integer
这个是什么问题?本地测试没问题,放空间报错

回复内容:

<code>function fetch($query='', $type = 'BOTH') 
{
    $result_type="PDO::FETCH_".$type;
    $query->setFetchMode($result_type);//这报错,$query是pdo的query结果
    $result_arr=$query->fetch();
    return $result_arr;
}
</code>

PDOStatement::setFetchMode(): SQLSTATE[HY000]: General error: mode must be an integer
这个是什么问题?本地测试没问题,放空间报错

no code, no truth.

你得把代码和数据库从你的项目中抽出来并尽量简化,以便于其它人在自己的电脑上重现你遇到的问题,再来分析。

PDO扩展在Windows环境和Linux环境的行为不相同,有的代码在Windows环境是不报错的(warning、notice全打开也不报),但在linux环境就会报错的,我7年前遇到过。


改一行代码:

<code>$query->setFetchMode(constant($result_type));
</code>

PDOStatement::setFetchMode方法必须传入第一个int型的mode参数啊,你肯定没传或者传的不是int型的。至于本地测试没报错,可能是service的错误输出级别跟空间的不一样。

<code>PDOStatement::setFetchMode ( int $mode );

//mode
//The fetch mode must be one of the PDO::FETCH_* constants.
</code>

一个更好的方法,有图有真相:

PDOStatement::setFetchMode()问题

你理解错文档的意思了,那是要你填一个PDO的常量,不是字符串。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn