Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MatteoOreficeIT/8071a6a325ab801d8456ebc4eb320db5 to your computer and use it in GitHub Desktop.
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 )
<?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