How to get rid of MySQL error 'Prepared statement needs to be re-prepared'

后端 未结 7 1477
夕颜
夕颜 2020-11-27 07:43

I\'ve rewritten my site php-code and added MySQL Stored Procedures.

In my local version everything works fine but after I uploaded my site to hosting server I\'m co

7条回答
  •  臣服心动
    2020-11-27 08:05

    @docwhat's answer seems nice, but on a shared hosting server, not everyone is allowed to touch the table_open_cache or table_definition_cache options.

    Since this error is related to prepared statements, I have tried to 'emulate' those with PDO by providing the following option:

    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, [
        PDO::ATTR_EMULATE_PREPARES => true
    ]);
    

    Note: actually this is in a Laravel 5.6 project, and I added the option in config/database.php:

    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            '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' => '',
            'strict' => true,
            'engine' => null,
            'options' => [
                PDO::ATTR_EMULATE_PREPARES => true,
            ],
        ],
        (...)
    ],
    

    I have not tested the impact of emulating prepared statements on the duration of loading my site, but it works against the error SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared I got.

    Update on the performance: the emulated version seems to be slightly faster (32.7±1.4ms emulated, 35.0±2.3ms normal, n=10, p-value=0.027 for two-tailed Student's T-test).

提交回复
热议问题