問題描述
我在 homestead 虛擬機 (vagrant) 中使用最新版本的 laravel (5.1).
我將我的項目連接到本地 mariaDB 服務器,其中有一些表和 2 個 db-view.
因為我只在 db-view 表上做了一些選擇,所以我隨機收到這個錯誤:
<塊引用>一般錯誤:1615 Prepared statement需要重新準備
從今天開始,僅在 db 視圖上進行選擇時,我總是會收到此錯誤.如果我打開我的 phpMyAdmin 并進行相同的選擇,它會返回正確的結果.
我嘗試打開 php artisan tinker
并選擇 db-view 的一條記錄,但它返回相同的錯誤:
//從用戶表中選擇一個用戶>>>$user = 新應用用戶=><應用用戶#000000006dc32a890000000129f667d2>{}>>>$user = AppUser::find(1);=><應用用戶#000000006dc32a9e0000000129f667d2>{編號:1,name: "盧卡",電子郵件:luca@email.it",客戶 ID:1,created_at: "2015-08-06 04:17:57",更新時間:2015-08-11 12:39:01"}>>>//從 Source db-view 中選擇一個源>>>$source = 新 AppSource=><AppSource #000000006dc32a820000000129f667d2>{}>>>$source = AppSource::find(1);IlluminateDatabaseQueryException 帶有消息SQLSTATE[HY000]:一般錯誤:1615 準備好的語句需要重新準備(SQL:select * from `sources` where `sources`.`id` = 1 limit 1)'
我該如何解決?我讀到了 mysqldump 的一個問題(但不是我的情況)并增加了 table_definition_cache
的值,但不確定它是否會起作用,我無法修改它們.
這是一種 Laravel 錯誤嗎?
我怎樣才能弄清楚?
<小時>按照要求,我添加了我的模型源代碼.源代碼:
hasMany("AppCustomerSource", "sourceId", "id");}/**** @return [類型] [說明]*/公共功能問題(){return $this->hasMany("AppIssue", "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->e??rror . ']');}dd($result->fetch_assoc());
<小時>
編輯 3:2個月后,我還在那里.同樣的錯誤,沒有找到解決方案.我決定在 aritsan tinker 中嘗試一些解決方案,但沒有好消息.我報告了我的嘗試:
首先嘗試獲取一個表模型:
<預><代碼>>>>$user = AppUser::find(1);=>應用用戶 {#697編號:1,name: "盧卡",電子郵件:luca.d@company.it",客戶 ID:1,created_at: "2015-08-06 04:17:57",更新時間:2015-10-27 11:28:14",}現在嘗試獲取一個視圖表模型:
<預><代碼>>>>$ir = AppContentRepository::find(15);IlluminateDatabaseQueryException 消息為SQLSTATE[42S02]:未找到基表或視圖:1146 表 'dbname.content_repositories' 不存在(SQL:從 `content_repositories` 中選擇 * 其中 `content_repositories`.`id` = 1限制 1)'當 contentRepository 在模型 ContentRepository.php 中沒有正確的表名設置時:
<預><代碼>>>>$pdo = DB::connection()->getPdo();=>PDO {#690交易中:假,錯誤信息:["00000",1146,"表 'dbname.content_repositories' 不存在",],屬性: [案例" =>天然,錯誤模式" =>例外,自動提交" =>1、持久"=>錯誤的,DRIVER_NAME" =>"mysql","SERVER_INFO" =>正常運行時間:2513397 線程:12 問題:85115742 慢查詢:6893568 打開:1596 刷新表:1 打開表:936 查詢每秒平均:33.864","ORACLE_NULLS" =>天然,"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" =>["PDO 聲明",],"EMULATE_PREPARES" =>0,"CONNECTION_STATUS" =>"通過 TCP/IP 的本地化","DEFAULT_FETCH_MODE" =>兩個都,],}>>>在模型 ContentRepository.php 中更改表值:
<預><代碼>>>>$ir = AppContentRepository::find(15);IlluminateDatabaseQueryException 帶有消息SQLSTATE[HY000]:一般錯誤:1615 準備好的語句需要重新準備(SQL:從 `contentRepository` 中選擇 *,其中 `contentRepository`.`id` = 15 limit 1)'正確時,注意缺少的errorInfo":
<預><代碼>>>>$pdo = DB::connection()->getPdo();=>PDO {#690交易中:假,屬性: [案例" =>天然,錯誤模式" =>例外,自動提交" =>1、持久"=>錯誤的,DRIVER_NAME" =>"mysql","SERVER_INFO" =>正常運行時間:2589441 線程:13 問題:89348013 慢查詢:7258017 打開:1604 刷新表:1 打開表:943 查詢每秒平均:34.504","ORACLE_NULLS" =>天然,"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" =>["PDO 聲明",],"EMULATE_PREPARES" =>0,"CONNECTION_STATUS" =>"通過 TCP/IP 的本地IPhere","DEFAULT_FETCH_MODE" =>兩個都,],}顯示數據庫的表:
<預><代碼>>>>$tables = DB::select('顯示表格');=>[{#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');IlluminateDatabaseQueryException 帶有消息SQLSTATE[HY000]:一般錯誤:1615 準備好的語句需要重新準備(SQL:select * from dbname.contentRepository limit 1)"嘗試未準備好的查詢:
<預><代碼>>>>DB::unprepared('select * from dbname.contentRepository limit 1')=>錯誤的嘗試第二次未準備好的查詢:
<預><代碼>>>>DB::unprepared('select * from dbname.contentRepository limit 1')IlluminateDatabaseQueryException 帶有消息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 是有效的回調,類 'IlluminateDatabaseMySqlConnection' 在/Users/luca/company/Laravel/dbname/vendor/laravel/framework/src 中沒有方法 'fetchAll'/Illuminate/Database/DatabaseManager.php 第 296 行嘗試第二個 PDOStatement::fetchAll():
<預><代碼>>>>$pdo::fetchAll('select * from dbname.contentRepository limit 1');[SymfonyComponentDebugExceptionFatalErrorException]調用未定義的方法 PDO::fetchAll()嘗試語句...:
<預><代碼>>>>$pdos = DB::statement('select * from dbname.contentRepository limit 1')IlluminateDatabaseQueryException 帶有消息SQLSTATE[HY000]:一般錯誤:1615 準備好的語句需要重新準備(SQL:select * from dbname.contentRepository limit 1)"謝謝
似乎可以添加
'options' =>[PDO::ATTR_EMULATE_PREPARES =>真的]
在數據庫配置中的projectName/config/database.php
文件中.會是這樣:
'mysql' =>['司機' =>'mysql','主機' =>環境('DB_HOST','本地主機'),'數據庫' =>env('DB_DATABASE', '偽造'),'用戶名' =>env('DB_USERNAME', '偽造'),'密碼' =>env('DB_PASSWORD', ''),'字符集' =>'utf8','整理' =>'utf8_unicode_ci','前綴' =>'','嚴格' =>錯誤的,'選項' =>[PDO::ATTR_EMULATE_PREPARES =>真的]],
Laravel 5.1.希望能幫到你!
我目前使用的是 Laravel 8,這個解決方案仍然有效.
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 AppUser
=> <AppUser #000000006dc32a890000000129f667d2> {}
>>> $user = AppUser::find(1);
=> <AppUser #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 AppSource
=> <AppSource #000000006dc32a820000000129f667d2> {}
>>> $source = AppSource::find(1);
IlluminateDatabaseQueryException 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 IlluminateDatabaseEloquentModel;
class Source extends Model
{
protected $table = 'sources';
/*
|--------------------------------------------------------------------------
| FOREIGN KEYS
|--------------------------------------------------------------------------
*/
/**
*
* @return [type] [description]
*/
public function customersList(){
return $this->hasMany("AppCustomerSource", "sourceId", "id");
}
/**
*
* @return [type] [description]
*/
public function issues(){
return $this->hasMany("AppIssue", "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 = AppUser::find(1);
=> AppUser {#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 = AppContentRepository::find(15);
IlluminateDatabaseQueryException 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 = AppContentRepository::find(15);
IlluminateDatabaseQueryException 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');
IlluminateDatabaseQueryException 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')
IlluminateDatabaseQueryException 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 'IlluminateDatabaseMySqlConnection' 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');
[SymfonyComponentDebugExceptionFatalErrorException]
Call to undefined method PDO::fetchAll()
Try statement... :
>>> $pdos = DB::statement('select * from dbname.contentRepository limit 1')
IlluminateDatabaseQueryException 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!
Edit: I'm currently on Laravel 8 and this solution is still working.
這篇關于Laravel:一般錯誤:1615 準備好的語句需要重新準備的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!