简体   繁体   中英

SQLSTATE[HY000]: General error: 2053

I am getting this error randomly in my PHP project. I am using Laravel framework. I googled around a bit and found that is an issue occurring due to 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. 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.

PS: A few runs means a random number and it does not occur at some specific point. My PHP version is 5.3.10 and I have got the same error on 5.4.4 as well.

Use:

DB::statement() instead of DB::query()

Hope it help you.

Actually what laravel suggests is using the correct statement for your operation

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

and of course select returns the selected results collection, this will always allow you to log and interperet your code better

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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