Connect to Ms SQL Server

You have probably figured it out by now, but just in case. I was also asked to pull data from an MS SQL server and present the data on a WordPress site. In my plugin, I stored the connection values (encrypted) as options. Here are the basics.

Connection Function:

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' );

Below is a sample query

$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;
        }