Query MS SQL Database with PHP

DETAILS

Below is a simple PHP script to demonstrate how to connect to a MS SQL database using an ODBC DSN.  You can find the SQL DSN information in the SQL Manager section in the control panel.

For more information on ODBC with PHP, please review the followings


 

<html>
<head><title>PHP SQL TEST</title></head>
<body>
<h2>MS SQL / DSN Test</h2>
<table border="1">

<?php
 $DSN="SQLDSN_sqlConn";
 $DSN_User="sqlusername";
 $DSN_Passwd="sqlpassword";
 $QueryString="SELECT id, FirstName, LastName, Email FROM tblContact";
 
 $Connect = odbc_connect( $DSN, $DSN_User, $DSN_Passwd );
 $Result = odbc_exec( $Connect, $QueryString );
 
 echo "\t" . "<tr>\n";
 echo "\t\t" . "<td>id</td><td>FirstName</td><td>LastName></td><td>Email</td>" . "\n";
 
 while( odbc_fetch_row( $Result ) )
 {
  $id  = odbc_result($Result,"id");
        $FirstName  = odbc_result($Result,"FirstName");
        $LastName  = odbc_result($Result,"LastName");
        $Email  = odbc_result($Result,"Email");

  echo "\t" . "<tr>\n";
  echo "\t\t" . "<td>" . $id . "</td><td>" . $FirstName . "</td><td>" . $LastName . "</td><td>" . $Email . "</td>\n";
  echo "\t" . "</tr>\n";
 }
 
 odbc_free_result( $Result );
 odbc_close( $Connect );
?>
</table>

  • 0 Usuários acharam útil
Esta resposta lhe foi útil?

Artigos Relacionados

Query a SQL database with ASP using a DSN-less connection

DETAILS<%Dim cnnSimple  ' ADO connectionDim rstSimple  ' ADO recordsetSet cnnSimple...

Send Email in ASP.NET

DETAILS<%@ Page Language="VB" %><script...

Redirect a subdomain to a subdirectory

DETAILS You can redirect a subdomain to a subdirectory by using ASP or ASP.net...

Query SQL database with .NET SQL Data Provider in ASP.NET

DETAILS<%@ import namespace="system.data.sqlclient" %><Script...

SQLXML code sample in ASP

DETAILS<%Response.ContentType = "text/xml"Dim oCmd, sSQL sSQL =...