Php Laravel:一般错误:1615准备好的语句需要重新准备
我正在homestead虚拟机(vagrant)中使用laravel(5.1)的最新版本 我将我的项目连接到本地mariaDB服务器,其中有一些表和2db视图 由于我在db view表上进行了一些仅选择,因此我随机收到此错误: 一般错误:需要重新准备1615准备好的语句 从今天起,我总是在db视图上选择时出现这个错误。 如果打开phpMyAdmin并进行相同的选择,则返回正确的结果 我试图打开Php Laravel:一般错误:1615准备好的语句需要重新准备,php,database,laravel,eloquent,mariadb,Php,Database,Laravel,Eloquent,Mariadb,我正在homestead虚拟机(vagrant)中使用laravel(5.1)的最新版本 我将我的项目连接到本地mariaDB服务器,其中有一些表和2db视图 由于我在db view表上进行了一些仅选择,因此我随机收到此错误: 一般错误:需要重新准备1615准备好的语句 从今天起,我总是在db视图上选择时出现这个错误。 如果打开phpMyAdmin并进行相同的选择,则返回正确的结果 我试图打开php artisan tinker并选择db视图的一条记录,但它返回相同的错误: // Select
php artisan tinker
并选择db视图的一条记录,但它返回相同的错误:
// 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)'
编辑2: 如果我使用mysqli在项目中执行相同的查询,它会工作:
$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());
编辑3: 两个月后,我还在那里。相同的错误,未找到解决方案。 我决定在Aristan tinker中尝试一些解决方案,但没有好消息。 我报告我所做的尝试: 首先尝试获取表模型:
>>> $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 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)'
现在尝试获取视图表模型:
>>> $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 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)'
当contentRepository模型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,
],
}
>>>
>>> $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)'
更改模型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,
],
}
>>>
>>> $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)'
如果正确,请注意缺少的“errorInfo”:
>>> $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,
],
}
显示数据库的表:
>>> $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",
},
]
尝试正常选择:
>>> $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)'
尝试未准备好的查询:
>>> DB::unprepared('select * from dbname.contentRepository limit 1')
=> false
>>> 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)'
第二次尝试未准备好的查询:
>>> DB::unprepared('select * from dbname.contentRepository limit 1')
=> false
>>> 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():
请尝试第二个PDO语句::fetchAll():
Try语句…:
>>> $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)'
谢谢这似乎是一个已经被记录在案的问题
编辑:
您的模型是否使用“id”作为主键?我喜欢在模型中显式地设置主键,即使它是
protected$primaryKey='id';//如果与id不同,则肯定需要在此处设置列
您还可以尝试注释掉
hasMany()
函数,然后重试。有时Laravel会在加载时做一些奇怪的事情,特别是当它试图映射到很多记录时 这似乎能起作用
'options' => [
\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
]
],
拉威尔5.1。希望这会有帮助 根据接受答案中的评论, 运行
设置全局表\u定义\u缓存=1024
最后,他解决了这个问题
请添加您的源代码模型。如问题中所述,我已经发现了该漏洞,但我有mariaDB(MySQL的分支),我无法增加该属性的价值。错误报告已暂停。@TENDOUSD93更新回答谢谢您的编辑。不,它不是主键。不管怎样,还是试试看,但不管用。我试图注释
customersList()
和issues()
方法,并调用$s=Source::find(1)代码>但它也不起作用。你的可填充列是什么?您可能有保留字或其他破坏查询的内容。您可以发布表结构吗?发布此消息后,您是否尝试过其他方法?运气好吗?请注意,这不应该被推荐,因为这可能会引入安全漏洞(SQL注入),因为仿真并非完美无缺。大多数解决方案建议在MySQL配置中增加table_definition_cache
的值。正如@David所提到的,SET GLOBAL table_definition_cache=1024
对我有效。适用于Laravel 5.6:-)关闭预处理语句模拟会导致其他错误。特别是,这一个是一个总显示停止:。在PHP7.2上,当保存在mysql十进制字段中时,浮点数被四舍五入为整数。在MariaDB中修复该错误之前,唯一的选择似乎是让表定义缓存大于服务器()上的表总数。该错误似乎特别影响视图。这个bug已经在这里提交了,MariaDB开发人员已经得到了一个补丁。在推出之前,从讨论中可以看出,表定义缓存的设置必须超过服务器上所有数据库中的表总数(从information_schema.tables;中选择COUNT(*))