<tfoot id='tm5SM'></tfoot>

  • <small id='tm5SM'></small><noframes id='tm5SM'>

  • <legend id='tm5SM'><style id='tm5SM'><dir id='tm5SM'><q id='tm5SM'></q></dir></style></legend>

        <bdo id='tm5SM'></bdo><ul id='tm5SM'></ul>
      <i id='tm5SM'><tr id='tm5SM'><dt id='tm5SM'><q id='tm5SM'><span id='tm5SM'><b id='tm5SM'><form id='tm5SM'><ins id='tm5SM'></ins><ul id='tm5SM'></ul><sub id='tm5SM'></sub></form><legend id='tm5SM'></legend><bdo id='tm5SM'><pre id='tm5SM'><center id='tm5SM'></center></pre></bdo></b><th id='tm5SM'></th></span></q></dt></tr></i><div id='tm5SM'><tfoot id='tm5SM'></tfoot><dl id='tm5SM'><fieldset id='tm5SM'></fieldset></dl></div>

      1. 使用 PDO 设置连接超时

        时间:2023-09-19

          <tbody id='8updR'></tbody>
          <bdo id='8updR'></bdo><ul id='8updR'></ul>
          <tfoot id='8updR'></tfoot>
        • <small id='8updR'></small><noframes id='8updR'>

          • <legend id='8updR'><style id='8updR'><dir id='8updR'><q id='8updR'></q></dir></style></legend>

              <i id='8updR'><tr id='8updR'><dt id='8updR'><q id='8updR'><span id='8updR'><b id='8updR'><form id='8updR'><ins id='8updR'></ins><ul id='8updR'></ul><sub id='8updR'></sub></form><legend id='8updR'></legend><bdo id='8updR'><pre id='8updR'><center id='8updR'></center></pre></bdo></b><th id='8updR'></th></span></q></dt></tr></i><div id='8updR'><tfoot id='8updR'></tfoot><dl id='8updR'><fieldset id='8updR'></fieldset></dl></div>

                  本文介绍了使用 PDO 设置连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 PDO 从 MySQL 服务器获取数据.我注意到的是:如果 MySQL 服务器不可用,这段代码需要真的(相对)很长时间才能返回异常:

                  I'm using PDO to get data off a MySQL server. What I noticed is this: if the MySQL server is unavailable, it takes really (relatively) long for this code to return an exception:

                  try {
                    $handle = new PDO($db_type . ':host='.$db_host.';dbname='.$db_name,$db_user,$db_pass);
                    // Tried using PDO::setAttribute and PDO::ATTR_TIMEOUT here
                  } catch(PDOException $e) {
                    echo $e->getMessage;
                  }
                  

                  在 MySQL 的情况下,异常发生只需要 2 分钟多一点(SQLSTATE[HY000] [2003] Can't connect to MySQL server on...)和 30 秒在 PostgreSQL (SQLSTATE[08006] [7] 超时已过期).

                  In case of MySQL it takes just over 2 minutes for the exception to occur (SQLSTATE[HY000] [2003] Can't connect to MySQL server on...) and 30 seconds on PostgreSQL (SQLSTATE[08006] [7] timeout expired).

                  我尝试使用 PDO::setAttribute 和 PDO::ATTR_TIMEOUT 但它不起作用.我想这是有道理的,因为问题发生在此语句之前.

                  I tried using PDO::setAttribute and PDO::ATTR_TIMEOUT but it's not working. Which I guess makes sense, since the problem occurs before this statement.

                  有没有办法设置连接数据库的超时时间?2 分钟/30 秒对我来说似乎很长,让 PDO 意识到那里什么都没有.

                  Is there a way to set a timeout for connecting to the DB? 2 minutes/30 seconds seems really long to me for PDO to realize there is nothing there.

                  我想我在某处看到过这种情况,但我一辈子都找不到了.

                  I think I saw this being done somewhere, but can't find it again for the life of me.

                  推荐答案

                  $DBH = new PDO(
                      "mysql:host=$host;dbname=$dbname", 
                      $username, 
                      $password,
                      array(
                          PDO::ATTR_TIMEOUT => 5, // in seconds
                          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
                      )
                  );
                  

                  这篇关于使用 PDO 设置连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从 mysql_connect() 获取 PHP PDO 连接? 下一篇:PDO 错误:SQLSTATE[HY000]:一般错误:2031

                  相关文章

                  最新文章

                  <i id='0lkRa'><tr id='0lkRa'><dt id='0lkRa'><q id='0lkRa'><span id='0lkRa'><b id='0lkRa'><form id='0lkRa'><ins id='0lkRa'></ins><ul id='0lkRa'></ul><sub id='0lkRa'></sub></form><legend id='0lkRa'></legend><bdo id='0lkRa'><pre id='0lkRa'><center id='0lkRa'></center></pre></bdo></b><th id='0lkRa'></th></span></q></dt></tr></i><div id='0lkRa'><tfoot id='0lkRa'></tfoot><dl id='0lkRa'><fieldset id='0lkRa'></fieldset></dl></div>

                  1. <legend id='0lkRa'><style id='0lkRa'><dir id='0lkRa'><q id='0lkRa'></q></dir></style></legend>

                      <bdo id='0lkRa'></bdo><ul id='0lkRa'></ul>

                    <small id='0lkRa'></small><noframes id='0lkRa'>

                      <tfoot id='0lkRa'></tfoot>