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:
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> | |
" . $id . " | " . $FirstName . " | " . $LastName . " | " . $Email . " |