We are trying to pull data from a second database into Wordpress. The secondary database is used internally to store data, and Wordpress (also only used internal) is partially used to display some of this data.

I did not find a reliable way to install Wordpress on SQL Server and thus decided to install it separately on MySQL (as intended) and pull in the extra required data from SQL Server. INSERT and UPDATE statement are also expected to be needed.

After some research online the 'best' I could find was to integrate a second ezSQL with a connection to the SQL Server.

Can I just use the default ezSQL 'configuration'?
Is this even possible, without resulting in compatible / interference issues?


$db = new ezSQL_sqlsrv($db_user, $db_password, $db_name, $db_host);
$current_time = $db->get_var("SELECT " . $db->sysdate() . " AS 'GetDate()'");


$wpdbtest_maindb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
$current_time = $wpdbtest_maindb->get_results("SELECT " . $db->sysdate());


  • Google found these two articles for me: bavotasan.com/2011/access-another-database-in-wordpress and webprogrammingblog.com/reading-external-database-wordpress . The first one has code that looks a lot like your 2nd code sample.
    – Pat J
    Jun 5 '13 at 20:21
  • Thank you! I already read those articles. But my question is not how to implement it but if a second instance of ezSQL will interfere with Wordpress's 'native' ezSQL?
    – Ben Z.
    Jun 6 '13 at 15:35
  • Because using that method (new wpdb) I can only connect to a second MySQL database, as far as I now.
    – Ben Z.
    Jun 6 '13 at 15:37
  • As I read the wpdb reference, wpdb is loosely based on ezSQL, so there is no 'native' ezSQL to worry about. (Even if there was, $mssql_wpdb = new wpdb( ... ); would create a new instance of the wpdb class, separate from WP's global $wpdb, so you would be in the clear. (Unless I'm completely misunderstanding PHP classes...)
    – Pat J
    Jun 6 '13 at 15:59
  • Also: did you run across this? stackoverflow.com/questions/2328224/…
    – Pat J
    Jun 6 '13 at 16:07

I apologize for the late reply, I was transferred to a new project and forgot about it. I ended up using a standard PHP PDO connection.

Here is an example:

// connection variables
$servername = "localhost";
$database = "database";
$user = "username";
$pass = "password";

try {
    //create connection
    $DBH = new PDO('sqlsrv:Server='.$servername.';Database='.$database, $user, $pass);

    //insert project
    $insert_data = array(
                'id' => $client_id,
                'name' => $client_name

    $STH = $DBH->prepare("INSERT INTO dbo.clients (id, name) VALUES (:id, :name); ");  

    // close the connection  
    $DBH = null;  
catch(PDOException $e)
    echo $e->getMessage();

For more information please see these links, or google PHP PDO.
https://stackoverflow.com/questions/2328224/how-do-i-retrieve-external-data-from-ms-sql-from-a-wordpress-blog (as suggested by Pat J)

  • Thanks for writing down your solution Ben Z. We would have done the same for our project but our webhosting company doesn't support connections to external servers..
    – Pitt
    Dec 5 '13 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.