Friday, May 15, 2009

Connecting MS SQL Server through PHP

Being a PHP developer, we've been used to connect to MySql Database through PHP. Using a MySql Database generally recommended strongly by overall community but what if I have some restrictions and have to use MS Sql Server.

Recently I came across a situation where one of my friend called me to clarify his few queries. He was working on moving asp application onto PHP (Kudos to PHP ! This has become a trend now -lol ) and he came across a condition where database previously used was MS SQL and his client didn't want to change the database though ( God knows why ?? ). It gave me a nice chance to have our own little class script which would handle the connectivity to the server and handle the errors as well as other DB related necessary functions.

Below is the class definition which I think you might find useful for your future use...


<?php

// SQL SERVER DATABASE LOGIN DETAILS
$DB_USER_MSSQL = username;
$DB_PASS_MSSQL = password;
$DB_HOST_MSSQL = host;
$DB_DATABASE_MSSQL = database;


class ConnectDB_MSSQL {
var $conn;
var $errFunc;

function defineerror($msg) {
echo "</table></table></table><p>\n";
echo "<h1>Database Error: $msg</h1>\n";
exit;
}
function ConnectDB_MSSQL($db_name,$server,$user,$pass,$error_function="defineerror") {
$this->errFunc = $error_function;
$this->conn = mssql_connect($server, $user, $pass);
if (! $this->conn ) {
$this->error("Unable to connect to MSSQL database");
return;
}
$result = @mssql_select_db($db_name, $this->conn);
if(!$result) {
$this->error("Unable to select MSSQL $database database");
}
}


function error($msg="") {
$this->{$this->errFunc}($msg);
}

function close() {
return @mssql_close($this->conn);
}

function query($sql) {
$result = @mssql_query($sql, $this->conn);
if(!$result) {
$this->error("Query ($sql) failed: ");
}
return new DatabaseQuery_MSSQL($result);
}


function addSlashes($string) {
return addslashes($string);
}

function stripSlashes($string) {
return stripslashes($string);
}

function parseDate($string) {
list($date, $time) = split(" ", $string);
list($year, $month, $day) = split("-", $date);
list($hour, $minute, $second) = split(":", $time);

$result = mktime($hour, $minute, $second, $month, $day, $year);

return $result;
}
}

class DatabaseQuery_MSSQL {
var $id;

function DatabaseQuery_MSSQL($id) {
$this->id = $id;
}

function fetchArray() {
return @mssql_fetch_array($this->id);
}

function numRows() {
return @mssql_num_rows($this->id);
}

function seek($row) {
return @mssql_data_seek($this->id, $row);
}

function fetchField($offset) {
return @mssql_fetch_field($this->id, $offset);
}

function f($offset) {
$a = @mssql_fetch_array($this->id);
return $a[$offset];
}

function fetchRow() {
return @mssql_fetch_row($this->id);
}

function numFields() {
return @mysql_num_fields($this->id);
}

function result($row, $index) {
return @mssql_result($this->id, $row, $index);
}

function freeResult() {
return @mssql_free_result($this->id);
}
}

?>


Object for the above class defined can be created as :

<?php

$db_MSSQL = new ConnectDB_MSSQL($DB_DATABASE_MSSQL,$DB_HOST_MSSQL,$DB_USER_MSSQL,$DB_PASS_MSSQL);

?>

and that's all !! Just create a library file of above class and include it in your script, and you are done !! Isn't it simple !!

BTW, sorry for formatting in above script.. I tried a lot to bring it to proper format, but thanks to blogspot... :)

Feel free to put your views or comments !! till then Aloha !!