Reputation: 179
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.
P.S.: 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.
Upvotes: 2
Views: 7934
Reputation: 1
Use excute ,not query. select returns the selected results collection, this will always allow you to log and interperet your code better
Upvotes: -1
Reputation: 41
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
Upvotes: 3
Reputation: 12059
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.
Upvotes: 0