php ADODB 在连接到 MySQL 之前调用 mysql_options()

发布于 2024-08-19 03:28:03 字数 652 浏览 4 评论 0原文

我的应用程序使用 ADODB 同时连接到 2 个 MySQL DB。应用程序正在下载大文件;这需要很长时间,所以会发生“mysql已经消失”错误。

我知道 MySQL 允许使用自动重新连接,可以使用 mysql_options() 启用它,但我不知道如何将此功能应用于 ADODB 适配器。

    $DB = NewADOConnection('mysql');
    $DB->Connect(DB_HOST, DB_LOGIN, DB_PASSWORD, DB_DBNAME);

谢谢您抽出时间!

PS:也许我应该问如何从 $DB 获取 DB 处理程序变量?如果我可以从 ADODO 获取数据库连接处理程序作为 $handler,我可以使用 mysql_options($handler, MYSQL_OPT_RECONNECT, 1);但是如果我应该在连接之前调用 mysql_options() 我怎样才能得到 $handler (根据 MySQL 参考 http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html )

My application is using ADODB to connect simultaneously to 2 MySQL DB. Application is downloading large files; it takes a lot of time so "mysql has gone away" error takes place.

I know that MySQL allows to use automatic reconnection which could be enabled using mysql_options(), but I have no clue how to apply this function to ADODB adapter.

    $DB = NewADOConnection('mysql');
    $DB->Connect(DB_HOST, DB_LOGIN, DB_PASSWORD, DB_DBNAME);

Thank you for you time!

PS: Probably I should ask how to get DB handler variable from $DB? If I could get DB connection handler from ADODO as $handler I could use mysql_options($handler, MYSQL_OPT_RECONNECT, 1); But how could I get $handler if I should call mysql_options() before connection (according to MySQL reference http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html )

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

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

发布评论

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

评论(1

何以心动 2024-08-26 03:28:03

要设置 mysql 选项,可以使用 dsn。这是我经常使用的连接模板:

$options = '';
if ($driver == 'mysql' OR $driver == 'mysqli')
{
  if ($params['pconnect'] === TRUE)
  {
    $options .= '?persist';
  }
  $flags = MYSQL_CLIENT_COMPRESS;
  $options .= (empty($options)?'?':'&')."clientflags=$flags";
}

$dsn = "{$driver}://{$username}:{$password}@{$hostname}/{$database}{$options}";

if ($driver == 'sqlite')
{
  if ($params['pconnect'] === TRUE)
  {
    $options = '?persist';
  }
  $database = urlencode($database);
  $dsn = "{$driver}://{$database}{$options}";
}

$adodb =& ADONewConnection($dsn);

if ($adodb)
{
  //set fetch mode
  $adodb->SetFetchMode(ADODB_FETCH_BOTH);

  //character set
  if ($driver == 'mysql' OR $driver == 'mysqli')
  {
    if (isset($params['char_set']) AND $params['char_set']
        AND isset($params['dbcollat']) AND $params['dbcollat'])
    {
      $charset    = $adodb->qstr($params['char_set']);
      $collation  = $adodb->qstr($params['dbcollat']);
      $adodb->Execute("SET NAMES $charset COLLATE $collation");
    }
  }
  if ($debug)
  {
    @ob_start();
    $adodb->debug = TRUE;
  }
}

如果您只将其用于 MySQL,则可以省略 sqlite 驱动程序条件块。
显然,PHP MySQL 客户端仅支持有限的选项,这里是列表 。你可以尝试MySQLi,它有更多选项

您是否尝试过持久连接?

To set mysql options, you can use dsn. This is my connection template that I always use:

$options = '';
if ($driver == 'mysql' OR $driver == 'mysqli')
{
  if ($params['pconnect'] === TRUE)
  {
    $options .= '?persist';
  }
  $flags = MYSQL_CLIENT_COMPRESS;
  $options .= (empty($options)?'?':'&')."clientflags=$flags";
}

$dsn = "{$driver}://{$username}:{$password}@{$hostname}/{$database}{$options}";

if ($driver == 'sqlite')
{
  if ($params['pconnect'] === TRUE)
  {
    $options = '?persist';
  }
  $database = urlencode($database);
  $dsn = "{$driver}://{$database}{$options}";
}

$adodb =& ADONewConnection($dsn);

if ($adodb)
{
  //set fetch mode
  $adodb->SetFetchMode(ADODB_FETCH_BOTH);

  //character set
  if ($driver == 'mysql' OR $driver == 'mysqli')
  {
    if (isset($params['char_set']) AND $params['char_set']
        AND isset($params['dbcollat']) AND $params['dbcollat'])
    {
      $charset    = $adodb->qstr($params['char_set']);
      $collation  = $adodb->qstr($params['dbcollat']);
      $adodb->Execute("SET NAMES $charset COLLATE $collation");
    }
  }
  if ($debug)
  {
    @ob_start();
    $adodb->debug = TRUE;
  }
}

You can omit the sqlite driver conditional block if you will only use this for MySQL.
Apparentlt, PHP MySQL client only support limited option, here is the list. You can try MySQLi, it have more options.

Have you try the persistent connection istead?

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