Query an MS SQL Database with PHP using DSNLess Connection

The information in this article applies to:

  • PHP
  • MS SQL 2000
  • MS SQL 2005

Details
Below is a simple PHP script to demonstrate how to connect to a MS SQL database using a DSNLess connection.

For more information on ODBC with PHP, please review the following:
PHP Builder tutorial - http://www.phpbuilder.com/columns/jayesh20021029.php3
PHP Documentation on ODBC connections - http://www.php.net/manual/en/ref.odbc.php

Sample code:

\n";
15echo "\t\t" . " " . "\n";
16
17while( odbc_fetch_row( $Result ) )
18{
19$id = odbc_result($Result,"id");
20$FirstName = odbc_result($Result,"FirstName");
21$LastName = odbc_result($Result,"LastName");
22$Email = odbc_result($Result,"Email");
23echo "\t" . " \n ";
24echo "\t\t" . " \n";
25echo "\t" . "\n";
26}
27
28odbc_free_result( $Result );
29odbc_close( $Connect );
30?>
31table>
32




 
1
2 <html>
3 <head><title>PHP SQL TESTtitle>head>
4 <body>
5 <h2>MS SQL / DSN Testh2>
6 <table border="1">
7 php
8 $ConnString="DRIVER={SQL Server};SERVER=;DATABASE=";
9 $DB_User="sqlusername";
10 $DB_Passwd="sqlpassword";
11 $QueryString="SELECT id, FirstName, LastName, Email FROM tblContact";
12 $Connect = odbc_connect( $ConnString, $DB_User, $DB_Passwd );
13 $Result = odbc_exec( $Connect, $QueryString );
14 echo "\t" . "
id FirstName LastName> Email
" . $id . " " . $FirstName . " " . $LastName . " " . $Email . "
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Sample Code on how to send email using PHP

Part of what makes the PHP mail() function is so simple is its lack of flexibility. Most...

Query MySQL Database in PHP

1 <HTML> 2 <HEAD> 3 4...