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);
?>

About these ads

31 thoughts on “How to connect MSSQL with php

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

  2. 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.”

  3. hi,
    i have installed SQL Server 2000 on windows server 2008.
    i have also installed php with apache on the same server but still i find it difficult to connect php with sql server.

    I am new to php, please help me….

    regards,
    mohit

  4. thank you very much for your article.

    Its very useful article for the beginner, those who are trying to connect mssql via php

  5. Thank you for an excellent article.

    I followed your instructions and connected first time to Windows NT Server 2003 from Windows XP running XAMPP !!

    XAMPP for windows installs ntwdblib.dll and php_mssql.dll in the right directories if you use the windows installer version of XAMPP, but if you use the ZIP file to install XAMPP they are not included and you have to manually put ntwdblib.dll into \xampp\apache\bin and manually put php_mssql.dll into \xampp\php\ext.

    I used versions that I had previously got with XAMPP when I used the windows installer version of XAMPP.

    John

  6. Hey plz reply ive tried everything but I keep getting the same error

    :Fatal error: Call to undefined function mssql_connect() in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\try.php on line 7

      • Hi,
        I have try following code but get same error.

        “Fatal error: Call to undefined function mssql_connect() in C:\wamp\www\rnd\conn.php ”

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

        $myServer = “MYSERVER\SQLEXPRESS”; // host/instance_name
        $myUser = “sa”; // username
        $myPass = “password”; // paasword
        $myDB = “db-live”; // 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);
        ?>

        Thanks,

  7. sir i connect mssql server 2005 above step but follwing error message can be display so i can’t connect.

    Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: MAC-PC\SQLEXPRESS in c:\wamp\www\krunal\mssql_connect_php\index.php on line 12
    Couldn’t connect to SQL Server on MAC-PC\SQLEXPRESS

    so i can’t connect. please help me.
    Follwing is the code

  8. Fatal error: Call to undefined function mssql_connect() in C:\wamp\www\test\sql server\index.php on line 11

    This is Error Occurring when trying to connect MSSQL 2000

  9. Warning: mssql_connect() [function.mssql-connect]: message: Login failed for user ‘NT AUTHORITY\ANONYMOUS LOGON’. (severity 14)

  10. Pingback: Php with MSSql | Jisku.com - Developers Network

  11. Good Tutotial ….

    But one thing missing there is that !

    DOWNLOAD The Latest dll(php_mssql.dll, ntwdblib.dll) files according to your PHP version……

  12. “How to connect MSSQL with php | I was born intellegent but education ruined me!
    !!” Just Blinds ended up being genuinely engaging and beneficial!

    In modern world that is really hard to deliver. Thanks a lot, Crystle

  13. Howdy outstanding blog! Does running a blog similar to
    this take a large amount of work? I’ve no expertise in coding however I was hoping to start my own blog soon. Anyhow, should you have any suggestions or techniques for new blog owners please share. I understand this is off topic nevertheless I simply needed to ask. Thanks a lot!

  14. I am curious to find out what blog system you happen to be using?
    I’m experiencing some minor security problems with my latest blog and I’d like to find something more
    secure. Do you have any recommendations?

  15. You may wonder how this can be accomplished even without
    dieting. To be fair, the same patterns set out by our guardiansto take upon ourselves the same routines of our
    parents. A wide variety of pure green coffee bean extract reviews, but how do you really know?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s