laravel SQLSTATE[HY000]:一般错误:2053
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13237655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQLSTATE[HY000]: General error: 2053
提问by Arpan Gupta
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:
我在我的 PHP 项目中随机收到此错误。我正在使用 Laravel 框架。我用谷歌搜索了一下,发现这是由于 PDO 引起的问题。我试图在错误发生之前记录我试图运行的查询,当我通过 MySQL 复制和运行相同的查询时,它运行得非常好。以下是代码片段:
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]: General error: 2053 in laravel/database/connection.php line 293。如果有人能给我一个合理的解决方案,这将是一个很大的帮助。
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.
PS:几次运行意味着一个随机数,它不会发生在某个特定点。我的 PHP 版本是 5.3.10,我在 5.4.4 上也遇到了同样的错误。
回答by Gaurav Dave
Use:
用:
DB::statement()
instead of DB::query()
DB::statement()
代替 DB::query()
Hope it help you.
希望对你有帮助。
回答by Vadg
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() 不返回更新、插入和删除所做的受影响的行响应
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
祝你好运
回答by Tim Withers
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.
使用参数化查询可能会更好并解决问题......也许吧。真的不知道你传递给它的数据。
回答by simpman
Use excute ,not query. select returns the selected results collection, this will always allow you to log and interperet your code better
使用执行,而不是查询。select 返回选定的结果集合,这将始终允许您更好地记录和解释您的代码