How to connect MSSQL with php

Please follow the steps given below to connect MSSQL with php:

1. Settings related to your php.ini file:

a) search the variable mssql.secure_connection in your php.ini file and put it to on mode if its off
b) remove comment from the dll extention php_mssql.dll (i.e. remove the ; from the front of the extention )

2. Settings related to the dll files.

download a file name ntwdblib.dll from the internet. you can download it from here or can search on internet for that. copy the downloaded dll to the apache/bin directory and for IIS copy it to the php extention directory (if path not known can be found in php.ini for variable extension_dir)

also you need to have your php_mssql.dll in your php extension directory. if its not present please download it and copy it to the default php extension directory.

3. restart all your services (i.e. php and apache or iis) and you can use the script given below to connect to your SQL Server.

<?php
//mssql.secure_connection = On
// Need to upload ntwdblib.dll from net

$myServer = “DVLP066\SQLEXPRESS”; // host/instance_name
$myUser = “sa”; // username
$myPass = “sadvlp066″; // paasword
$myDB = “myfraiche”; // database name

// connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die(”Couldn’t connect to SQL Server on $myServer”);

// select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die(”Couldn’t open database $myDB”);

echo “You are connected to the ” . $myDB . ” database on the ” . $myServer . “.”;

$query = “SELECT top 10 * FROM tbl_customers”; // your database query
$result = mssql_query($query);
while($row = mssql_fetch_assoc($result))
{
print_r($row);
}
// close the connection
mssql_close($dbhandle);
?>

Posted in PHP. Tags: , , , , . 2 Comments »

2 Responses to “How to connect MSSQL with php”

  1. PHP-г MSSQL-тэй холбох « Xocoo’s Weblog Says:

    [...] How to connect MSSQL with php Tags: [...]

  2. Bandish Says:

    thank you for this article

    Now I am able to connect remote ms-sql server to post and retrive data.

    but the problem is after these 3 steps i am not able to open localhost/phpmyadmin

    I am using ubuntu 8 as OS
    netbeans 6.5 as ide for php
    and xampp for linux

    i get following error message

    1 starting xampp: “root@bandish-desktop:/home/bandish# /opt/lampp/lampp start
    Starting XAMPP for Linux 1.6.8a…
    PHP Warning: PHP Startup: Unable to load dynamic library ‘/opt/lampp/lib/php/extensions/no-debug-non-zts-20060613/php_mssql.dll’ – /opt/lampp/lib/php/extensions/no-debug-non-zts-20060613/php_mssql.dll: cannot open shared object file: No such file or directory in Unknown on line 0
    XAMPP: Starting Apache with SSL (and PHP5)…
    XAMPP: Starting MySQL…
    XAMPP: Starting ProFTPD…
    XAMPP for Linux started.

    2. while opening localhost: ” Existing configuration file (./config.inc.php) is not readable.”


Leave a Reply