Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Monday, 20 June 2011

Migrating From MySQL to SQL Server running PHP on IIS


In the following code, we connect to the MySQL server with mysql_connect() and then select the database with mysql_select_db() that we will work against.
MySQL PHP
MySQL Actor Table Viewer
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'pass@word1';
$database = 'sakila';
$table = 'actor';
 
if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");
 
if (!mysql_select_db($database))
    die("Can't select database");
SQL Server PHP
With SQL Server PHP, the database context is established in one call to sqlsrv_connect() that returns a connection handle for running a queries.
SQL Server Actor Table Viewer
$db_host = '.\SQLEXPRESS';
$db_user = 'sa';   //recommend using a lower privileged user
$db_pwd = 'pass@word1';
$database = 'sakila';
$table = 'actor';
 
$connectionInfo = array("UID" => $db_user, "PWD" => $db_pwd, "Database"=>$database); 
$conn = sqlsrv_connect( $db_host, $connectionInfo);
if( !$conn )
{
     echo "Connection could not be established.\n";
     die( print_r( sqlsrv_errors(), true));
}