Last active
September 19, 2018 13:22
-
-
Save MatteoOreficeIT/8071a6a325ab801d8456ebc4eb320db5 to your computer and use it in GitHub Desktop.
How to seamlessly execute MySQL LOCK TABLE statements in Laravel:5.4.* Query Builder ( also PHP PDO )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// THIS causes a [Illuminate\Database\QueryException] exception : | |
// 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: LOCK TABLES imports WRITE) | |
// see: https://stackoverflow.com/questions/31582445/table-locking-issues-with-laravel-5-1 | |
\DB::statement('LOCK TABLES mytable WRITE'); | |
// USE THIS INSTEAD | |
\DB::unprepared('LOCK TABLES mytable WRITE'); | |
// OR | |
\DB::getPdo()->exec('LOCK TABLES mytable WRITE'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment