MySQL 查询上的连接重置

发布于 2024-09-03 03:29:08 字数 3270 浏览 10 评论 0原文

好吧,我很困惑。 我正在尝试(本地)对数据库执行查询,但不断收到连接重置错误。我一直在通用 DAO 类中使用下面的方法来构建查询字符串并传递给 Zend_Db API。

public function insert($params) {
    $loop = false;
    $keys = $values = '';
    foreach($params as $k => $v){
        if($loop == true){
            $keys   .= ',';
            $values .= ',';
        }
        $keys   .= $this->db->quoteIdentifier($k);
        $values .= $this->db->quote($v);
        $loop = true;
    }

    $sql = "INSERT INTO " . $this->table_name . " ($keys) VALUES ($values)";

    //formatResult returns an array of info regarding the status and any result sets of the query
    //I've commented that method call out anyway, so I don't think it's that
    try {
        $this->db->query($sql);
        return $this->formatResult(array(
                true,
                'New record inserted into: '.$this->table_name
        ));
    }catch(PDOException $e) {
        return $this->formatResult($e);
    }
}

到目前为止,这一切都很好 - 自从我们生成新表来记录用户输入以来,错误一直在发生。插入字符串如下所示:

INSERT INTO tablename(`id`,`title`,`summary`,`description`,`keywords`,`type_id`,`categories`) VALUES ('5539','Sample Title','Sample content','
\'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In et pellentesque mauris. Curabitur hendrerit, leo id ultrices pellentesque, est purus mattis ligula, vitae imperdiet neque ligula bibendum sapien. Curabitur aliquet nisi et odio pharetra tincidunt. Phasellus sed iaculis nisl. Fusce commodo mauris et purus vehicula dictum. Nulla feugiat molestie accumsan. Donec fermentum libero in risus tempus elementum aliquam et magna. Fusce vitae sem metus. Aenean commodo pharetra risus, nec pellentesque augue ullamcorper nec. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nullam vel elit libero. Vestibulum in turpis nunc.\'','this,is,a,sample,array',1,'category title') 

以下是在组装查询 (var_dump) 之前获取的参数:

array
    'id' => string '1' (length=4)
    'title' => string 'Sample Title' (length=12)
    'summary' => string 'Sample content' (length=14)
    'description' => string '<p>'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In et pellentesque mauris. Curabitur hendrerit, leo id ultrices pellentesque, est purus mattis ligula, vitae imperdiet neque ligula bibendum sapien. Curabitur aliquet nisi et odio pharetra tincidunt. Phasellus sed iaculis nisl. Fusce commodo mauris et purus vehicula dictum. Nulla feugiat molestie accumsan. Donec fermentum libero in risus tempus elementum aliquam et magna. Fusce vitae sem metus. Aenean commodo pharetra risus, nec pellentesque augue'... (length=677)
    'keywords' => string 'this,is,a,sample,array' (length=22)
    'type_id' => int 1
    'categories' => string 'category title' (length=43)

下一个调用端口是检查表上的限制,因为如果“描述”的长度在 300 左右,它似乎会插入标记(在 310 - 330 之间变化)。字段限制设置为 VARCHAR(1500),并且该字段的验证不允许任何超过 1200(使用 HTML)的值,800(不使用 HTML)的值。

真正的问题是,如果我获取这个 sql 字符串并通过命令行执行它,它就可以正常工作 - 所以我一辈子都无法弄清楚出了什么问题。

我尝试扩展服务器参数,即 意外连接重置:PHP 或 Apache 问题?

所以,简而言之,我被难住了。有什么想法吗?

OK, I'm flummoxed.
I'm trying to execute a query on a database (locally) and I keep getting a connection reset error. I've been using the method below in a generic DAO class to build a query string and pass to Zend_Db API.

public function insert($params) {
    $loop = false;
    $keys = $values = '';
    foreach($params as $k => $v){
        if($loop == true){
            $keys   .= ',';
            $values .= ',';
        }
        $keys   .= $this->db->quoteIdentifier($k);
        $values .= $this->db->quote($v);
        $loop = true;
    }

    $sql = "INSERT INTO " . $this->table_name . " ($keys) VALUES ($values)";

    //formatResult returns an array of info regarding the status and any result sets of the query
    //I've commented that method call out anyway, so I don't think it's that
    try {
        $this->db->query($sql);
        return $this->formatResult(array(
                true,
                'New record inserted into: '.$this->table_name
        ));
    }catch(PDOException $e) {
        return $this->formatResult($e);
    }
}

So far, this has worked fine - the errors have been occurring since we generated new tables to record user input. The insert string looks like this:

INSERT INTO tablename(`id`,`title`,`summary`,`description`,`keywords`,`type_id`,`categories`) VALUES ('5539','Sample Title','Sample content','
\'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In et pellentesque mauris. Curabitur hendrerit, leo id ultrices pellentesque, est purus mattis ligula, vitae imperdiet neque ligula bibendum sapien. Curabitur aliquet nisi et odio pharetra tincidunt. Phasellus sed iaculis nisl. Fusce commodo mauris et purus vehicula dictum. Nulla feugiat molestie accumsan. Donec fermentum libero in risus tempus elementum aliquam et magna. Fusce vitae sem metus. Aenean commodo pharetra risus, nec pellentesque augue ullamcorper nec. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nullam vel elit libero. Vestibulum in turpis nunc.\'','this,is,a,sample,array',1,'category title') 

Here are the parameters it's getting before assembling the query (var_dump):

array
    'id' => string '1' (length=4)
    'title' => string 'Sample Title' (length=12)
    'summary' => string 'Sample content' (length=14)
    'description' => string '<p>'Lorem ipsum dolor sit amet, consectetur adipiscing elit. In et pellentesque mauris. Curabitur hendrerit, leo id ultrices pellentesque, est purus mattis ligula, vitae imperdiet neque ligula bibendum sapien. Curabitur aliquet nisi et odio pharetra tincidunt. Phasellus sed iaculis nisl. Fusce commodo mauris et purus vehicula dictum. Nulla feugiat molestie accumsan. Donec fermentum libero in risus tempus elementum aliquam et magna. Fusce vitae sem metus. Aenean commodo pharetra risus, nec pellentesque augue'... (length=677)
    'keywords' => string 'this,is,a,sample,array' (length=22)
    'type_id' => int 1
    'categories' => string 'category title' (length=43)

The next port of call was checking the limits on the table, since it seems to insert if the length of "description" is around the 300 mark (it varies between 310 - 330). The field limit is set to VARCHAR(1500) and the validation on this field won't allow anything past bigger than 1200 with HTML, 800 without.

The real kicker is that if I take this sql string and execute it via the command line, it works fine - so I can't for the life of me figure out what's wrong.

I've tried extending the server parameters i.e.
Unexpected Connection Reset: A PHP or an Apache issue?

So, in a nutshell, I'm stumped. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执笏见 2024-09-10 03:29:08

好的,对于遇到此问题的其他人来说 - 问题与 Zend_Db_Statement 有关 - 或者更确切地说与该组件所依赖的 preg 库有关。它已在 Zend 问题跟踪器中标记为: http://framework.zend.com/issues /browse/ZF-8399,但严格来说这并不是 Zend lib 的问题。根据您的服务器环境,您可能会也可能不会遇到此问题。就我而言,此错误发生在:

Win XP,
瓦普2
PHP 5.3

可以通过使用

db->getConnection()->exec($sql)

直接通过适配器(PDO 等)有效执行 sql 来解决。或者,您可以修改您的服务器以纠正 PCRE 本身的段错误(尽管我承认我不完全确定您会如何解决此问题)。希望能帮助其他遇到同样问题的人

OK, for anyone else experiencing this problem - the issue is to do with Zend_Db_Statement - or rather the preg library that this component relies on. It's been flagged in the Zend issue tracker: http://framework.zend.com/issues/browse/ZF-8399, but its not strictly a problem with the Zend lib. You may or may not experience this problem depending on your server environment. In my case, this error occurred on:

Win XP,
Wamp 2
PHP 5.3

It can be resolved by using

db->getConnection()->exec($sql)

which effectively executes the sql directly via the adapter (PDO etc). Alternatively, you can modify your server to rectify segfaulting in PCRE itself (though I confess I'm not entirely sure how you'd go about this). Hope that helps anyone else with the same issue

最舍不得你 2024-09-10 03:29:08

对于那些在谷歌上搜索并找到这篇关于在执行 MySQL 查询时重置连接的帖子的人:

除了 sunwukung 的答案之外,还请检查您的 MySQL 设置中是否存在瓶颈,尤其是这个:

max_allowed_packet = 1M

您可以将其更改为较大的数字。这是我的:

max_allowed_packet = 32M

现在可以了。

说明:

从设置名称中可以看出,连接重置主要是由于进出MySQL的数据集太大,从而“终止”与MySQL的连接。

For those who are googling and find this post about connection reset when doing MySQL query:

Apart from the answer from sunwukung, please also check that if there is a bottle-neck in your MySQL setting, especially this one:

max_allowed_packet = 1M

You can change it to a larger number. Here is mine:

max_allowed_packet = 32M

Now it works.

Explanation:

As you can read from the setting name, connection reset is mainly the result of too large data-set going in and out of MySQL, which "Terminate" the connection to MySQL.

莫相离 2024-09-10 03:29:08

您能否发布确切的错误消息(如果您使用 PDO,则为 $this->db->e​​rrorInfo() )?
您确定已建立数据库连接吗?

Could you post exact error message ($this->db->errorInfo() if you use PDO) ?
Are you sure that you have db connection established?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文