blog

Home / DeveloperSection / Blogs / Accessing SQL server database with PHP

Accessing SQL server database with PHP

Anonymous User4816 12-Mar-2012

The Microsoft SQL Server 2005 Driver for PHP allows PHP developers to access data in SQL Server databases. The driver includes support for Windows and SQL Server Authentication methods, transactions, parameter binding, streaming, metadata access, connection pooling, and error handling.

To access the data from SQL server database you have to perform some changes in php.ini file.

Change 1: Put the extension file (php_sqlsrv.dll or php_sqlsrv_ts.dll) in the PHP extension directory. If you are running the non-thread-safe version of PHP (php5.dll), you should use the non-thread-safe version of the driver (php_sqlsrv.dll).

Change 2:  Uncomment the extension=php_sqlsrv.dll or php_pdo_mssql.dll in php.ini file.

After performing these changes restart your web server.

Example:

Here I’m creating an example for connecting PHP script with SQL server. In this example, simply I’m retrieving data from MS-SQL table and print it.

  

<?php
               // SQL server connection string information
                $serverName = "XXXX";
                $userName = "XXXX";
                $password = "XXXX";
                $db_Name = "MyDatabase";
                // connect with SQL server database
                $conn = mssql_connect($serverName, $userName, $password)
                        or die ("Database cannot connected successfully");
           // Select SQL database
                $selectDB = mssql_select_db($db_Name, $conn)
                            or die("Database cannot be opended");
                 // SQL server query
                $qry  = "select Name, ContactNo, EmailId from tblLogin";
                // Execute Query
                $qry_result = mssql_query($qry);
      // exatract table value
                while($row = mssql_fetch_array($qry_result))
                {
                    // print table value
                 echo $row['Name'].'\t\t';
                     echo $row['ContactNo'].'\t\t';
                     echo $row['EmailId'];
              }
                // Close connection with SQL server database
                mssql_close();
        ?>


Updated 18-Sep-2014
I am a content writter !

Leave Comment

Comments

Liked By