你现在可能已经明白了,但以防万一。我还被要求从MS SQL server中提取数据,并将数据显示在WordPress网站上。在我的插件中,我将连接值(加密)存储为选项。以下是基本知识。
连接功能:
public function rimsdb() {
global $rimsdb;
$options = get_option(\'lwd_gs_plugin_options\');
$serverName = $options[\'db_host\']; //serverName\\instanceName
$connectionInfo = array("Database" => $options[\'db_name\'], "UID" => $options[\'db_user\'], "PWD" => $options[\'db_password\']);
$rimsdb = sqlsrv_connect($serverName, $connectionInfo);
if ($rimsdb) {
echo "Connection established.<br />";
} else {
echo "Connection could not be established.<br />";
die(print_r(sqlsrv_errors(), true));
}
}
add_action( \'init\', \'rimsdb\' );
下面是一个示例查询
$params = array(\'my_value\');
$sql1 = "SELECT col1, col2, col3
FROM mytable
WHERE col1 = ?";
$stmt = sqlsrv_query($rimsdb, $sql1, $params);
sqlsrv_execute($stmt);
if ($stmt === false) {
die(print_r(sqlsrv_errors(), true));
}
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$values = $row;
}