douzen1880 2018-03-13 18:05
浏览 53

如何使用wp-config.php设置在PHP网站上连接到SQL Server

I can connect to SQL server from a PHP website using the following code:

  <?php
  /*
   * Follow me on Twitter: @HertogJanR
   * Please donate: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=4EFPSXA623NZA
   */

  $connectionInfo = array( "UID" => "wordpress", "PWD" => "xxxxxxx", "Database" => "wordpress" );
  $link = sqlsrv_connect( ".", $connectionInfo );
  if( $link ) {
       echo "Connection established.<br />";
  } else{
       echo "Connection could not be established.<br />";
       die( print_r( sqlsrv_errors(), true ) );
  }
  $sql = "SELECT table_name FROM information_schema.tables";

  $stmt = sqlsrv_query( $link, $sql );
  while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC ) ) {
    echo $row['table_name']."<br />";
  }

  if( $stmt === false ) {
    die( print_r( sqlsrv_errors(), true));
  }
  ?>

However , i can't get connected using these credentials in the wp-config.php

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpress');

/** MySQL database password */
define('DB_PASSWORD', 'xxxxx');

/** MySQL hostname */
define('DB_HOST', '.'); /* localhost doesn't work either */

The error is: Warning: mysqli_real_connect(): (HY000/2002): Unknown errror while connecting in C:\wordpress\wp-includes\wp-db.php on line 1531

When i change it to localhost the error is different: Warning: mysqli_real_connect(): (HY000/2002): No connection could be made because the target machine actively refused it. in C:\wordpress\wp-includes\wp-db.php on line 1531

Any ideas? The wp-config.php looks like it has variables setup to connect to MySQL How do i tell it to use SQL Server?

  • 写回答

1条回答 默认 最新

  • dongwu1410 2018-03-13 18:14
    关注

    Why dont you just try using wpdb? This is what you should use to work with secondary databases in WP.

    $mydb = new wpdb('username','password','database','localhost');
    $rows = $mydb->get_results("select Name from my_table");
    echo "<ul>";
    foreach ($rows as $obj) :
    echo "<li>".$obj->Name."</li>";
    endforeach;
    echo "</ul>";
    
    评论

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接