Magento – How to connect make MS Sql Server Connection in Magento

databasemagento-1.9magento-connectmssql

I need to make connection in magento say a php file where i can make connection to MS SQL Server. can anyone please let me know how can i do this.

Best Answer

As Magento is heavily tied to MySQL you will need to use plain PHP or try it with the Zend Framework DB Adapter: Check out the mssql_connect function on php.net and Zend_Db_Adapter.

Also you need to make sure the needed MSSQL extension is enabled in your php.ini file, for example extension=php_mssql.dll

To store the username, password and host I suggest you use the same way in app/etc/local.xml just as Magento does it. You can then afterwards access the data in your PHP file.

app/etc/local.xml example:

    <resources>
        <default_setup>
            <!-- your default mysql setup here -->
        </default_setup>
        <mssql_setup>
            <connection>
                <host><![CDATA[localhost]]></host>
                <username><![CDATA[dbuser]]></username>
                <password><![CDATA[dbpassword]]></password>
                <dbname><![CDATA[dbname]]></dbname>
            </connection>
        </mssql_setup>
    </resources>

access the data from local.xml to use in your PHP script:

$host = (string)$config->global->resources->mssql_setup->connection->host;
$username = (string)$config->global->resources->mssql_setup->connection->username,
$password = (string)$config->global->resources->mssql_setup->connection->password;
$dbname = (string)$config->global->resources->mssql_setup->connection->dbname;
Related Topic