SQLSTATE[HY000]: General error: mode must be an integer

 4610

今天在写自定义mvc框架的时候遇到一个问题:

  1. SQLSTATE[HY000]: General error: mode must be an integer

下面贴上代码:

  1. //这是报错位置的代码
  2. public function dao_query($sql,$all = true){
  3.     try{
  4.         $stmt = $this->pdo->query($sql);
  5.         //设置fetch_mode         
  6.          
  7.         $stmt->setFetchMode($this->fetch_mode);//这里报错
  8.         //解析数据
  9.         if(!$all){
  10.             return $stmt->fetch();
  11.         }else{
  12.             return $stmt->fetchAll();
  13.         }
  14.     }catch(PDOException $e){
  15.         $this->dao_exception($e);
  16.     }
  17. }

代码提示:General error: mode must be an integer,我又去翻了一下文档

  1. PDOStatement::fetchAll([ int $fetch_style [, mixed $fetch_argument   [, array $ctor_args = array()  ]]] ) : array

这里标注$fetch_style必须为int型,后来我直接将 $this->fetch_mode 直接改成 PDO::FETCH_ASSOC,发现运行成功,后来翻到我的config里面存储的竟然是个字符串类型:

  1. 'database'=>array(
  2.     'type'=>'mysql',
  3.     'host'=>'localhost',
  4.     'port'=>'3306',
  5.     'user'=>'root',
  6.     'pass'=>'root',
  7.     'charset'=>'utf8',
  8.     'dbname'=>'my_database',
  9.     'fetch_mode'=>'PDO::FETCH_ASSOC',//这里的问题
  10.     'prefix'=>''
  11. ),

不过为了美观,我还是决定不改config了,于是................:

  1. $stmt->setFetchMode(constant($this->fetch_mode));//这里必须使用int $model  constant($this->fetch_mode)

文档的解释:

  1. constant    ( string $name   ) : mixed

通过 name 返回常量的值。

当你不知道常量名,却需要获取常量的值时,constant() 就很有用了。也就是常量名储存在一个变量里,或者由函数返回常量名。

返回常量的值。如果常量未定义则返回 NULL。



TAG标签:
本文网址:https://www.zztuku.com/detail-8729.html
站长图库 - SQLSTATE[HY000]: General error: mode must be an integer
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐

    卡哇伊动物头像矢量素材