Laravel:一般错误:1615准备的语句需要重新准备 [英] Laravel: General error: 1615 Prepared statement needs to be re-prepared
问题描述
我在宅基虚拟机(vagrant)中使用最新版本的laravel(5.1)。
我将项目连接到本地的mariaDB服务器,我有一些表和2 db-view。
由于我只对db-view表做了一些选择,我随机收到这个错误:
一般错误:1615准备好的语句需要重新准备
从今天起,我总是得到这个错误时,只有在数据库视图选择。
如果我打开我的phpMyAdmin并进行相同的选择它返回正确的结果。
我试图打开 php artisan tinker
并选择db-view的一个记录,但它返回相同的错误:
//选择一个用户from user table
>>>> $ user = new App\User
=> < App\User#000000006dc32a890000000129f667d2> {}
>>> $ user = App\User :: find(1);
=> < App\User#000000006dc32a9e0000000129f667d2> {
id:1,
name:Luca,
电子邮件:luca@email.it,
customerId:1,
created_at: 08-06 04:17:57,
updated_at:2015-08-11 12:39:01
}
>>>
//从Source选择一个源db-view
>>> $ source = new App\Source
=> < App\Source#000000006dc32a820000000129f667d2> {}
>>> $ source = App\Source :: find(1);
Illuminate \Database\QueryException与消息'SQLSTATE [HY000]:一般错误:1615准备的语句需要重新准备(SQL:select *从`sources`其中`sources`.`id` = 1限制1)'
如何解决?
我读了一个问题与mysqldump(但不是在我的情况下),并增加 table_definition_cache
的价值,但它不确定它会工作,我可以'
这是一种laravel错误吗?
/ p>
编辑:
码。
Source.php:
<?php
namespace App;
使用Illuminate \Database\Eloquent\Model;
类源扩展模型
{
protected $ table ='sources';
/ *
| ------------------------------- -------------------------------------------
|外键
| ------------------------------------------- -------------------------------
* /
/ **
*
* @return [type] [description]
* /
public function customersList(){
return $ this-> hasMany(App\CustomerSource ,sourceId,id);
}
/ **
*
* @return [type] [description]
* /
public function issues(){
return $ this-> hasMany(App\Issue,sourceId,id);
}
}
编辑2:
如果我用mysqli在项目中执行相同的查询,它工作:
$ db = new mysqli(getenv('DB_HOST'),getenv('DB_USERNAME'),getenv('DB_PASSWORD'),getenv('DB_DATABASE'));
if($ db-> connect_errno> 0){
dd('无法连接到数据库['。$ db-> connect_error。']');
}
$ sql =SELECT * FROM`sources` WHERE`id` = 4;
if(!$ result = $ db-> query($ sql)){
dd('运行查询['。$ db-> error。'] ;
}
dd($ result-> fetch_assoc());
编辑3:
Afeter 2个月,我还在那里。相同的错误,找不到解决方案。
我决定尝试一个小解决方案在aritsan修补匠,但没有好消息。
我报告我试过的:
首先尝试获取一个表模型:
>>> $ user = \App\User :: find(1);
=> App\User {#697
id:1,
name:Luca,
email:luca.d@company.it,
customerId:1,
created_at:2015-08-06 04:17:57,
updated_at:2015-10-27 11:28:14,
}
现在尝试获取视图表模型:
>>>> $ ir = \App\ContentRepository :: find(15);
Illuminate \Database\QueryException,带有消息'SQLSTATE [42S02]:找不到基表或视图:1146'dbname.content_repositories'表不存在(SQL:select * from`content_repositories`其中`content_repositories` .`id` = 1 limit 1)'
当contentRepository没有正确的表名设置模型ContentRepository.php:
>>> $ pdo = DB :: connection() - > getPdo();
=> PDO {#690
inTransaction:false,
errorInfo:[
00000,
1146,
表'dbname.content_repositories'不存在
],
属性:[
CASE=> NATURAL,
ERRMODE=> EXCEPTION,
AUTOCOMMIT=> 1,
PERSISTENT=> false,
DRIVER_NAME=> mysql,
SERVER_INFO=> Uptime:2513397主题:12问题:85115742慢查询:6893568打开:1596 Flush表:1打开表:936每秒查询平均值:33.864,
ORACLE_NULLS=& NATURAL,
CLIENT_VERSION=> mysqlnd 5.0.11-dev - 20120503 - $ Id:id_here $,
SERVER_VERSION=> 5.5.5-10.0.17-MariaDB-1〜wheezy-wsrep-log,
STATEMENT_CLASS=> [
PDOStatement,
],
EMULATE_PREPARES=> 0,
CONNECTION_STATUS=> localiphere via TCP / IP,
DEFAULT_FETCH_MODE=> BOTH,
],
}
>>>
CHANGE TABLE VALUE INSIDE模型ContentRepository.php:
>>> $ ir = \App\ContentRepository :: find(15);
Illuminate \Database\QueryException与消息'SQLSTATE [HY000]:一般错误:1615准备的语句需要重新准备(SQL:select *从`contentRepository`其中`contentRepository`.`id` = 15限制1)'
如果正确,注意缺少errorInfo :
>>> $ pdo = DB :: connection() - > getPdo();
=> PDO {#690
inTransaction:false,
属性:[
CASE=> NATURAL,
ERRMODE=> EXCEPTION,
AUTOCOMMIT=> 1,
PERSISTENT=> false,
DRIVER_NAME=> mysql,
SERVER_INFO=> Uptime:2589441主题:13问题:89348013慢查询:7258017打开:1604 Flush表:1打开表:943每秒查询平均值:34.504,
ORACLE_NULLS=& NATURAL,
CLIENT_VERSION=> mysqlnd 5.0.11-dev - 20120503 - $ Id:id_here $,
SERVER_VERSION=> 5.5.5-10.0.17-MariaDB-1〜wheezy-wsrep-log,
STATEMENT_CLASS=> [
PDOStatement,
],
EMULATE_PREPARES=> 0,
CONNECTION_STATUS=> localIPhere via TCP / IP,
DEFAULT_FETCH_MODE=> BOTH,
],
}
显示数据库表:
>>> $ tables = DB :: select('SHOW TABLES');
=> [
{#702
+Tables_in_dbname:table_name_there,
},
{#683
+Tables_in_dbname:table_name_there,
_name:table_name_there,
},
{#701
+Tables_in_dbname:table_name_there-20150917- 1159,
},
{#704
+Tables_in_dbname:contentRepository,* / VIEW TABLE IS THERE! / *
},
{#707
+Tables_in_dbname:table_name_there,
},
{#684
+Tables_in_dbname: table_name_there,
},
]
/ p>
>>> $ results = DB :: select('select * from dbname.contentRepository limit 1');
Illuminate \Database\QueryException,带有消息'SQLSTATE [HY000]:一般错误:1615准备好的语句需要重新准备(SQL:select * from dbname.contentRepository limit 1)'
尝试无准备的查询:
>>>> DB :: unprepared('select * from dbname.contentRepository limit 1')
=> false
尝试第二次无准备的查询:
>>> DB :: unprepared('select * from dbname.contentRepository limit 1')
Illuminate \Database\QueryException,带有消息'SQLSTATE [HY000]:常规错误:2014无法执行查询,而其他未缓冲的查询处于活动状态。考虑使用PDOStatement :: fetchAll()。或者,如果您的代码只是针对mysql运行,您可以通过设置PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY属性来启用查询缓冲。 (SQL:select * from dbname.contentRepository limit 1)'
尝试PDOStatement :: fetchAll
>>> DB :: fetchAll('select * from dbname.contentRepository limit 1');
PHP警告:call_user_func_array()期望参数1是一个有效的回调,类'Illuminate\Database\MySqlConnection'在/ Users / luca / company / Laravel / dbname / vendor中没有方法'fetchAll' /laravel/framework/src/Illuminate/Database/DatabaseManager.php on line 296
尝试第二个PDOStatement: :fetchAll():
>>> $ pdo :: fetchAll('select * from dbname.contentRepository limit 1');
[Symfony\Component\Debug\Exception\FatalErrorException]
调用未定义的方法PDO :: fetchAll()
尝试语句...:
>> $ pdos = DB :: statement('select * from dbname.contentRepository limit 1')
Illuminate \Database\QueryException与消息'SQLSTATE [HY000]:一般错误:1615准备的语句需要重新准备(SQL:select * from dbname.contentRepository limit 1)'
谢谢
似乎正在添加
'=> [
\ PDO :: ATTR_EMULATE_PREPARES => true
]
内部 projectName / config / database.php
文件。它将是这样:
'mysql'=> [
'driver'=> 'mysql',
'host'=> env('DB_HOST','localhost'),
'database'=> env('DB_DATABASE','forge'),
'username'=> env('DB_USERNAME','forge'),
'password'=> env('DB_PASSWORD',''),
'charset'=> 'utf8',
'collation'=> 'utf8_unicode_ci',
'prefix'=> '',
'strict'=> false,
'options'=> [
\ PDO :: ATTR_EMULATE_PREPARES => true
]
],
<希望它会帮助!
I'm using last version of laravel (5.1) in a homestead virtual machine (vagrant).
I connect my project to a local mariaDB server, in which I have some table and 2 db-view.
Since I made some select only on the db-view tables, I receive back randomly this error:
General error: 1615 Prepared statement needs to be re-prepared
From today, I always get this error when made select only on the db views. If I open my phpMyAdmin and make the same select it return the correct result.
I tried to open php artisan tinker
and select one record of the db-view but it return the same error:
// Select one user from user table
>>> $user = new App\User
=> <App\User #000000006dc32a890000000129f667d2> {}
>>> $user = App\User::find(1);
=> <App\User #000000006dc32a9e0000000129f667d2> {
id: 1,
name: "Luca",
email: "luca@email.it",
customerId: 1,
created_at: "2015-08-06 04:17:57",
updated_at: "2015-08-11 12:39:01"
}
>>>
// Select one source from Source db-view
>>> $source = new App\Source
=> <App\Source #000000006dc32a820000000129f667d2> {}
>>> $source = App\Source::find(1);
Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared (SQL: select * from `sources` where `sources`.`id` = 1 limit 1)'
How can I fix that?
I read about a problem with mysqldump (but not in my case) and to increase value of table_definition_cache
but it is not sure that it will work and I can't modify them.
Is this a kind of laravel bug?
How can I figure that out?
Edit:
As asked, I add my model source code. Source.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Source extends Model
{
protected $table = 'sources';
/*
|--------------------------------------------------------------------------
| FOREIGN KEYS
|--------------------------------------------------------------------------
*/
/**
*
* @return [type] [description]
*/
public function customersList(){
return $this->hasMany("App\CustomerSource", "sourceId", "id");
}
/**
*
* @return [type] [description]
*/
public function issues(){
return $this->hasMany("App\Issue", "sourceId", "id");
}
}
Edit 2:
If I execute the same query in the project with mysqli it works:
$db = new mysqli(getenv('DB_HOST'), getenv('DB_USERNAME'), getenv('DB_PASSWORD'), getenv('DB_DATABASE'));
if($db->connect_errno > 0){
dd('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM `sources` WHERE `id` = 4";
if(!$result = $db->query($sql)){
dd('There was an error running the query [' . $db->error . ']');
}
dd($result->fetch_assoc());
EDIT 3: Afeter 2 month, I'm still there. Same error and no solution found. I decide to try a little solution in aritsan tinker but no good news. I report what I've tried:
First try to fetch a table model:
>>> $user = \App\User::find(1);
=> App\User {#697
id: 1,
name: "Luca",
email: "luca.d@company.it",
customerId: 1,
created_at: "2015-08-06 04:17:57",
updated_at: "2015-10-27 11:28:14",
}
Now try to fetch a view table model:
>>> $ir = \App\ContentRepository::find(15);
Illuminate\Database\QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'dbname.content_repositories' doesn't exist (SQL: select * from `content_repositories` where `content_repositories`.`id` = 1 limit 1)'
When contentRepository doesn't have correct table name setup inside the model ContentRepository.php:
>>> $pdo = DB::connection()->getPdo();
=> PDO {#690
inTransaction: false,
errorInfo: [
"00000",
1146,
"Table 'dbname.content_repositories' doesn't exist",
],
attributes: [
"CASE" => NATURAL,
"ERRMODE" => EXCEPTION,
"AUTOCOMMIT" => 1,
"PERSISTENT" => false,
"DRIVER_NAME" => "mysql",
"SERVER_INFO" => "Uptime: 2513397 Threads: 12 Questions: 85115742 Slow queries: 6893568 Opens: 1596 Flush tables: 1 Open tables: 936 Queries per second avg: 33.864",
"ORACLE_NULLS" => NATURAL,
"CLIENT_VERSION" => "mysqlnd 5.0.11-dev - 20120503 - $Id: id_here $",
"SERVER_VERSION" => "5.5.5-10.0.17-MariaDB-1~wheezy-wsrep-log",
"STATEMENT_CLASS" => [
"PDOStatement",
],
"EMULATE_PREPARES" => 0,
"CONNECTION_STATUS" => "localiphere via TCP/IP",
"DEFAULT_FETCH_MODE" => BOTH,
],
}
>>>
CHANGE TABLE VALUE INSIDE model ContentRepository.php:
>>> $ir = \App\ContentRepository::find(15);
Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared (SQL: select * from `contentRepository` where `contentRepository`.`id` = 15 limit 1)'
When it is correct, pay attention to "errorInfo" that is missing:
>>> $pdo = DB::connection()->getPdo();
=> PDO {#690
inTransaction: false,
attributes: [
"CASE" => NATURAL,
"ERRMODE" => EXCEPTION,
"AUTOCOMMIT" => 1,
"PERSISTENT" => false,
"DRIVER_NAME" => "mysql",
"SERVER_INFO" => "Uptime: 2589441 Threads: 13 Questions: 89348013 Slow queries: 7258017 Opens: 1604 Flush tables: 1 Open tables: 943 Queries per second avg: 34.504",
"ORACLE_NULLS" => NATURAL,
"CLIENT_VERSION" => "mysqlnd 5.0.11-dev - 20120503 - $Id: id_here $",
"SERVER_VERSION" => "5.5.5-10.0.17-MariaDB-1~wheezy-wsrep-log",
"STATEMENT_CLASS" => [
"PDOStatement",
],
"EMULATE_PREPARES" => 0,
"CONNECTION_STATUS" => "localIPhere via TCP/IP",
"DEFAULT_FETCH_MODE" => BOTH,
],
}
Show db's tables:
>>> $tables = DB::select('SHOW TABLES');
=> [
{#702
+"Tables_in_dbname": "table_name_there",
},
{#683
+"Tables_in_dbname": "table_name_there",
},
{#699
+"Tables_in_dbname": "table_name_there",
},
{#701
+"Tables_in_dbname": "table_name_there-20150917-1159",
},
{#704
+"Tables_in_dbname": "contentRepository", */ VIEW TABLE IS THERE!!!! /*
},
{#707
+"Tables_in_dbname": "table_name_there",
},
{#684
+"Tables_in_dbname": "table_name_there",
},
]
Try with normal select:
>>> $results = DB::select('select * from dbname.contentRepository limit 1');
Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared (SQL: select * from dbname.contentRepository limit 1)'
Try unprepared query:
>>> DB::unprepared('select * from dbname.contentRepository limit 1')
=> false
Try second time unprepared query:
>>> DB::unprepared('select * from dbname.contentRepository limit 1')
Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. (SQL: select * from dbname.contentRepository limit 1)'
Try PDOStatement::fetchAll():
>>> DB::fetchAll('select * from dbname.contentRepository limit 1');
PHP warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Database\MySqlConnection' does not have a method 'fetchAll' in /Users/luca/company/Laravel/dbname/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php on line 296
Try second PDOStatement::fetchAll():
>>> $pdo::fetchAll('select * from dbname.contentRepository limit 1');
[Symfony\Component\Debug\Exception\FatalErrorException]
Call to undefined method PDO::fetchAll()
Try statement... :
>>> $pdos = DB::statement('select * from dbname.contentRepository limit 1')
Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared (SQL: select * from dbname.contentRepository limit 1)'
Thank you
It seems to work adding
'options' => [
\PDO::ATTR_EMULATE_PREPARES => true
]
Inside projectName/config/database.php
file in DB configuration. It will be like this:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'options' => [
\PDO::ATTR_EMULATE_PREPARES => true
]
],
Laravel 5.1. Hope it will help!
这篇关于Laravel:一般错误:1615准备的语句需要重新准备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!