4

My Laravel application (version 5.0.33) has suddenly started throwing the following error:

local.ERROR: exception 'PDOException' 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.' in vendor/laravel/framework/src/Illuminate/Database/Connection.php:296

Looking at the relevant line in Connection.php, it appears to be already using fetchAll:

 public function select($query, $bindings = array(), $useReadPdo = true)
        {
                return $this->run($query, $bindings, function($me, $query, $bindings) use ($useReadPdo)
                {
                        if ($me->pretending()) return array();

                        // For select statements, we'll simply execute the query and return an array
                        // of the database result set. Each element in the array will be a single
                        // row from the database table, and will either be an array or objects.
                        $statement = $this->getPdoForSelect($useReadPdo)->prepare($query);

                        $statement->execute($me->prepareBindings($bindings));

                        return $statement->fetchAll($me->getFetchMode());
                });
        }

I also added the following to my config/database.php:

'options' => [ PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true ]

But I'm still getting the same error. This application has been working correctly for years, and I hadn't made any changes to the code, so I don't know why this suddenly stared happening. I'm also new to Laravel and didn't write the original code, so I'm not even sure where to start debugging the issue. Can anyone help?

2
  • does this error occurs frequently ? Commented Jan 13, 2022 at 5:29
  • This is happening to me on aws code deploy, but I am trying to change the charset. Commented Feb 15, 2022 at 21:38

1 Answer 1

7

I had the same problem with a query in package datatable yajra/laravel-datatables in hosting server, changing the charset to 'charset' => 'utf8mb4', collation 'collation' => 'utf8mb4_unicode_ci' and some options work with me, full config:

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => array(
                PDO::ATTR_EMULATE_PREPARES => true,
                PDO::MYSQL_ATTR_LOCAL_INFILE => true,
                PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
            ),
        ],

ref : Laravel and MemSQL columnstore, unbuffered queries are active and https://www.eversql.com/mysql-utf8-vs-utf8mb4-whats-the-difference-between-utf8-and-utf8mb4/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!!! adding ``` PDO::ATTR_EMULATE_PREPARES => true, PDO::MYSQL_ATTR_LOCAL_INFILE => true, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true``` worked for me.. but just wondering if is this considered a bad fix?
Saved me too on a stupid Cpanel without easyApache, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.