我可以使用以下代码从PHP网站连接到SQL服务器:
<?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));
}
?>
但是,我无法使用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 */
错误是: 警告:mysqli_real_connect():( HY000 / 2002):在第1531行的C:\ wordpress \ wp-includes \ wp-db.php中连接时出现未知错误
当我将其更改为localhost时,错误是不同的: 警告:mysqli_real_connect():( HY000 / 2002):无法建立连接,因为目标计算机主动拒绝它。在第1531行的C:\ wordpress \ wp-includes \ wp-db.php
有什么想法吗? wp-config.php看起来像是有连接到MySQL的变量设置 我如何告诉它使用SQL Server?
答案 0 :(得分:1)
为什么不尝试使用wpdb?这是您在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>";