小編給大家分享一下遇到SQLSTATE[HY000]: General error: mode must be an integer的問題怎么辦,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
今天在寫自定義mvc框架的時候遇到一個問題:
SQLSTATE[HY000]: General error: mode must be an integer
下面貼上代碼:
//這是報錯位置的代碼
public function dao_query($sql,$all = true){
try{
$stmt = $this->pdo->query($sql);
//設置fetch_mode
$stmt->setFetchMode($this->fetch_mode);//這里報錯
//解析數據
if(!$all){
return $stmt->fetch();
}else{
return $stmt->fetchAll();
}
}catch(PDOException $e){
$this->dao_exception($e);
}
}代碼提示:General error: mode must be an integer,我又去翻了一下文檔
PDOStatement::fetchAll([ int $fetch_style [, mixed $fetch_argument [, array $ctor_args = array() ]]] ) : array
這里標注$fetch_style必須為int型,后來我直接將$this->fetch_mode直接改成PDO::FETCH_ASSOC,發現運行成功,后來翻到我的config里面存儲的竟然是個字符串類型:
'database'=>array( 'type'=>'mysql', 'host'=>'localhost', 'port'=>'3306', 'user'=>'root', 'pass'=>'root', 'charset'=>'utf8', 'dbname'=>'my_database', 'fetch_mode'=>'PDO::FETCH_ASSOC',//這里的問題 'prefix'=>'' ),
不過為了美觀,我還是決定不改config了,于是................:
$stmt->setFetchMode(constant($this->fetch_mode));//這里必須使用int $model constant($this->fetch_mode)
文檔的解釋:
constant ( string $name ) : mixed
通過 name 返回常量的值。
當你不知道常量名,卻需要獲取常量的值時,constant() 就很有用了。也就是常量名儲存在一個變量里,或者由函數返回常量名。
返回常量的值。如果常量未定義則返回 NULL。
看完了這篇文章,相信你對“遇到SQLSTATE[HY000]: General error: mode must be an integer的問題怎么辦”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。