Query an MS SQL Database with PHP using DSNLess Connection Print

  • 0

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:

 
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" . "
";
15 echo "\t\t" . " id FirstName LastName> Email " . "\n";
16
17 while( 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");
23 echo "\t" . " \n ";
24 echo "\t\t" . " " . $id . " " . $FirstName . " " . $LastName . " " . $Email . " \n";
25 echo "\t" . "\n ";
26 }
27
28 odbc_free_result( $Result );
29 odbc_close( $Connect );
30 ?>
31 table>
32





Was this answer helpful?

« Back