Laravel 5.4提供了错误的COM_STMT_PREPARE响应大小

时间:2017-05-12 13:09:49

标签: mysql laravel mariadb laravel-5.4 mysql-connector

我在网站上工作,过去一个月工作正常,昨天突然崩溃并说Wrong COM_STMT_PREPARE response size. Received 7

enter image description here

这是我在控制器中的代码:

public function newsFeed()
{
    // get all data needed for the news page

    try{

        $news = DB::SELECT("SELECT 
                                n.newId
                                ,n.title_en
                                ,n.title_es
                                ,n.description_en
                                ,n.description_es
                                ,n.newMainImg
                                ,n.tags
                                ,DATE_FORMAT(n.createDate, '%b - %e - %Y') as publishDate_en
                                ,DATE_FORMAT(n.createDate, '%e - %b - %Y') as publishDate_es
                                ,concat(u.firstName,' ',u.lastName) as author
                            FROM NEWS n
                              inner join USERS u
                                      on u.userId = n.author
                                     and u.statusId = 1
                            order by n.createDate desc limit 20"); 


        $favorites = DB::SELECT("SELECT 
                                    n.newId
                                    ,n.title_en
                                    ,n.title_es
                                    ,n.newMainImg
                                FROM
                                    NEWS n
                                WHERE viewsCount > 0
                                  and viewsCount is not null
                                order by viewsCount desc limit 6");


        $data = array(
            'news' => $news,
            'favorites' => $favorites
        );

        return view('news/newsFeed')->with('data', $data);

    }catch(Exception $exc){

        echo $exc->getMessage();
    }      

}

我还没找到任何解决方案。任何帮助将不胜感激。

更新

如果服务器位于000webhost有帮助,我发现有很多人遇到同样的问题。

更新2

错误说问题出在Connection.php第647行

这是代码:

protected function run($query, $bindings, Closure $callback)
{
    $this->reconnectIfMissingConnection();

    $start = microtime(true);

    // Here we will run this query. If an exception occurs we'll determine if it was
    // caused by a connection that has been lost. If that is the cause, we'll try
    // to re-establish connection and re-run the query with a fresh connection.
    try {
        $result = $this->runQueryCallback($query, $bindings, $callback);
    } catch (QueryException $e) {
        $result = $this->handleQueryException(
            $e, $query, $bindings, $callback
        );
    }

    // Once we have run the query we will calculate the time that it took to run and
    // then log the query, bindings, and execution time so we will report them on
    // the event that the developer needs them. We'll log time in milliseconds.
    $this->logQuery(
        $query, $bindings, $this->getElapsedTime($start)
    );

    return $result;
}

protected function runQueryCallback($query, $bindings, Closure $callback)
{
    // To execute the statement, we'll simply call the callback, which will actually
    // run the SQL against the PDO connection. Then we can calculate the time it
    // took to execute and log the query SQL, bindings and time in our memory.
    try {
        $result = $callback($query, $bindings);
    }

    // If an exception occurs when attempting to run a query, we'll format the error
    // message to include the bindings with SQL, which will make this exception a
    // lot more helpful to the developer instead of just the database's errors.
    catch (Exception $e) {
        throw new QueryException(
            $query, $this->prepareBindings($bindings), $e
        );
    }

    return $result;
}

2 个答案:

答案 0 :(得分:28)

我通过将PDO属性PDO::ATTR_EMULATE_PREPARES设置为true来解决此问题。您可以在config / database.php下为数据库连接添加一个选项。

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', 'localhost'),
    //.......
    'options'   => [PDO::ATTR_EMULATE_PREPARES => true,]
],

答案 1 :(得分:0)

我在Laravel 7.5 + PHP 7.4上遇到了同样的错误,但在pcntl_fork()之后。 错误日志显示此错误来自子进程中同时运行的代码的不同部分。

在所有子进程中重新连接到DB(仅在分叉之后)解决了此问题。