PHPJavascript

Web Development Guide – My Personal Library of Tutorials and Scripts

PHP MSSQL or SQL – Connect and configure

December16

Learn how to configure and connect PHP with Microsoft SQL. And you always thought PHP could only fit with MYSQL.

Requirements

PHP (Click here to Install and configure PHP5 on windows system)

MS SQL (Click here to Install MS SQL on windows)

Configure MS SQL with PHP on windows

Open php.ini file and add extension=php_mssql.dll . In most cases this line has already been added on php.ini file all you have to do is uncomment it ie remove the semi colon (;).

Note: php.ini file is usually located at C:/windows folder else search for it on your computer.

Make sure ntwdblib.dll is present in PHP folder (which is mostly C:/PHP) and windows system32 folder(C:/windows/system32), restart your computer before checking.

To use the MSSQL extension on Unix/Linux, you first need to build and install the FreeTDS library. Source code and installation instructions are available at the FreeTDS home page.

Test

To check if PHP has configured MS-SQL. Restart the computer and then use php_info() function to test in the following fashion:

  • Open notepad or dreamweaver++
  • Type <?php phpinfo() ?> and save as info.php
  • Place this file at C:\Inetpub\wwwroot\
  • Open internet explorer or mozilla firefox++ and type http://localhost/info.php

If configuration was successful you will find a similar section on MS-SQL:

mssql

MSSQL Support enabled
Active Persistent Links 0
Active Links 0
Library version 7.0
Directive Local Value Master Value
mssql.allow_persistent On On
mssql.batchsize 0 0
mssql.compatability_mode Off Off
mssql.connect_timeout 5 5
mssql.datetimeconvert On On

Connect MS SQL and PHP

Now lets learn how to connect to SQL server using PHP

<?php
$location="localhost"; //Hostname of MSSQL server
$username="sa"; //Username for MSSQL
$password=""; //password for MSSQL user
$database="pubs"; //Database name

$conn=mssql_connect("$location","$username","$password"); //connect to sql server

mssql_select_db($database,$conn);//select database
$res=mssql_query("select * from authors"); //select table
while($row=mssql_fetch_array($res)) { //fetch each row
echo $row["au_fname"]." ".$row["phone"];//print field au_fname and phone
}
?>

Note: Database “pubs” and table “authors” are created by default on MS-SQL server

posted under MySQL, PHP

Email will not be published

Website example

Your Comment:

 

2,885 spam comments
blocked by
Akismet