遇到SQLSTATE[HY000]: General error: mode must be an integer的问题怎么办

发布时间:2021-03-12 11:05:44 作者:小新
来源:亿速云 阅读:356

小编给大家分享一下遇到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的问题怎么办”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. mongodb配置
  2. oracle sql

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sql

上一篇:wms和tms的区别有哪些

下一篇:Discuz怎么支持大文件上传

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》