Connecting MS-SQL Server From PHP

Connecting MS-SQL Server From PHP

There are 2 ways to achieve this:
1. ODBC function
2. MSSQL functions

1. ODBC function
For using this, we need to set the DSN name by go to
Start > Settings > Control Panel > Administrative Tools > Data Sources (ODBC).

After we set up DSN name, we can follwoing function to connect the MS-SQL server

odbc_connect($dsn,$name,$pass);

$dsn - dsn name we created
$name - User Name For MS-SQL Server 200
$pass - password

Then we can execute the query using odbc_exec
$connect = odbc_connect($dsn,$name,$pass);;
$query = “SELECT uname FROM user”;

$exec = odbc_exec($connect, $query);

Then we need to get the results.we can achieve this using following code.

while(odbc_fetch_row($exec)){
$name = odbc_result($exec,”uname”);
echo “$name”; }
odbc_close($connect);
}

odbc_result($exec,”uname”) - here uname is field name

2. MSSQL functions

For using MSSQL function we need to do the following things
1. We need to install the Microsoft SQL Client Tools
2. We need to edit in php.ini as
extension=php?_mssql.dll
3. We need to put the php_mssql.dll file in both the /extensions and /WINNT/system32 folder.

After we set up these things we can use the mssql function in the manual.Example mssql functions are.
1. mssql_connect - to connect the mssql
2. mssql_query - Send MS SQL query
3. mssql_fetch_array - to fetch the array

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.