简体   繁体   English

SQLSTATE [HY000]:一般错误:2053

[英]SQLSTATE[HY000]: General error: 2053

I am getting this error randomly in my PHP project. 我在PHP项目中随机收到此错误。 I am using Laravel framework. 我正在使用Laravel框架。 I googled around a bit and found that is an issue occurring due to PDO. 我在Google上搜索了一下,发现这是由于PDO引起的问题。 I tried to log the query I am trying to run just before the error occurs and when I copy and run the same query through MySQL, it runs absolutely fine. 我试图记录错误发生之前我要运行的查询,并且当我通过My​​SQL复制并运行相同的查询时,它运行得很好。 Following is the code snippet: 以下是代码段:

foreach($batches as $batch){
        $query_post = "INSERT INTO posts (`fb_post_id`, `page_id`, `from_user_id`,`to_user_id`, ".
                        "`object_id`, `from`,`to`, `message`, `picture`,`link`, `icon`, `type`,`name`, ".
                        "`status_type`, `privacy`,`caption`, `description`,`story`, `actions`, `created_time`, ".
                        "`comment_count`,`like_count`, `created_at`) VALUES ";
        $query = '';
        foreach($batch as $row){
            $comma_separated = implode("','", $row);
            $query .= "('".$comma_separated."'),";
        }
        $query_post .= $query;
        $query_post= substr($query_post, 0, -1);
        $query_post= utf8_encode($query_post);
        Log::write('info', ' POST QUERY : '.$query_post);
        DB::query($query_post);
    }

After a few runs in the loop, I get the error: SQLSTATE[HY000]: General error: 2053 in laravel/database/connection.php line 293. It would be a great help if someone could give me a sound solution for it. 在循环中运行了几次之后,出现错误:SQLSTATE [HY000]:常规错误:laravel / database / connection.php第293行中的2053。如果有人可以给我一个合理的解决方案,那将是一个很大的帮助。

PS: A few runs means a random number and it does not occur at some specific point. PS:几次跑步意味着一个随机数,并且在某些特定时间点不会发生。 My PHP version is 5.3.10 and I have got the same error on 5.4.4 as well. 我的PHP版本是5.3.10,我在5.4.4上也遇到了同样的错误。

Use: 采用:

DB::statement() instead of DB::query() DB::statement()而不是DB::query()

Hope it help you. 希望对您有帮助。

Actually what laravel suggests is using the correct statement for your operation 其实laravel建议的是对您的操作使用正确的语句

DB::delete()
DB::update()
DB::insert()
DB::select()

the reason is because statement() does not return the affected rows response which update, insert and delete does 原因是因为statement()不返回受影响的行响应,而update,insert和delete会这样做

and of course select returns the selected results collection, this will always allow you to log and interperet your code better 当然select会返回选定的结果集合,这将始终使您能够更好地记录和交互代码

Best of luck 祝你好运

What about this? 那这个呢?

foreach($batches as $batch){
    $query_post = "INSERT INTO posts (`fb_post_id`, `page_id`, `from_user_id`,`to_user_id`, ".
                    "`object_id`, `from`,`to`, `message`, `picture`,`link`, `icon`, `type`,`name`, ".
                    "`status_type`, `privacy`,`caption`, `description`,`story`, `actions`, `created_time`, ".
                    "`comment_count`,`like_count`, `created_at`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    Log::write('info', ' POST QUERY : '.$query_post);
    DB::query($query_post,$batch);
}

Using parameter-ized queries might be better and fix the issue... maybe. 使用参数化查询可能会更好,并且可以解决问题……也许。 Don't really know the data you are passing into it. 真的不知道您要传递给它的数据。

Use excute ,not query. 使用批判,而不是查询。 select returns the selected results collection, this will always allow you to log and interperet your code better select返回选定的结果集合,这将始终使您能够更好地记录和交互代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM