Skip to content


Using Microsoft SQL Server with PHP

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

Using Microsoft SQL Server with PHP

Postby Web Guru » April 28th, 2009, 6:40 am

SQL Server 2005 Driver for PHP

PHP is an easy-to-learn and powerful open source scripting language. Microsoft SQL Server is a robust database product, which handles terabytes of data. Combine them to create database-driven Web sites that can handle large amounts of data and traffic.

The SQL Server Driver for PHP v1.0 is designed to enable reliable, scalable integration with SQL Server for PHP applications deployed on the Windows platform. The Driver for PHP is a PHP 5 extension that allows the reading and writing of SQL Server data from within PHP scripts. It provides a procedural interface for accessing data in all Editions of SQL Server 2005 and SQL Server 2008 (including Express Editions), and makes use of PHP features, including PHP streams to read and write large objects.

Download Microsoft SQL Server Driver for PHP:
http://www.microsoft.com/downloads/details.aspx?FamilyId=61BF87E0-D031-466B-B09A-6597C21A2E2A&displaylang=en

Blog for Microsoft SQL Server Driver with PHP
http://blogs.msdn.com/sqlphp/
User avatar
Web Guru
 
Posts: 94
Joined: March 24th, 2008, 7:59 am
Location: Lahore, Pakistan


Connecting SQL Server 2000 with PHP

Postby Web Guru » April 28th, 2009, 6:43 am

The following code enable you to connect the Micorosoft SQL Server 2000 database with PHP. For latest versions of SQL Server, use the PHP extension provided by Microsoft. (http://www.geosourcecode.com/using-microsoft-sql-server-with-php-t534.html)

Code: Select all
<?php
$myServer = "localhost";
$myUser = "your_name";
$myPass = "your_password";
$myDB = "examples";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT id, name, year ";
$query .= "FROM cars ";
$query .= "WHERE name='BMW'";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

//display the results
while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>
User avatar
Web Guru
 
Posts: 94
Joined: March 24th, 2008, 7:59 am
Location: Lahore, Pakistan


Connecting SQL Server 2000 & PHP using DSN

Postby Web Guru » April 28th, 2009, 6:45 am

DSN stands for 'Data Source Name'. It is an easy way to assign useful and easily rememberable names to data sources which may not be limited to databases alone. If you do not know how to set up a system DSN read our tutorial How to set up a system DSN.

Code: Select all
<?php

//connect to a DSN "myDSN"
$conn = odbc_connect('myDSN','','');

if ($conn)
{
  //the SQL statement that will query the database
  $query = "select * from cars";
  //perform the query
  $result=odbc_exec($conn, $query);

  echo "<table border=\"1\"><tr>";

  //print field name
  $colName = odbc_num_fields($result);
  for ($j=1; $j<= $colName; $j++)
  {
    echo "<th>";
    echo odbc_field_name ($result, $j );
    echo "</th>";
  }

  //fetch tha data from the database
  while(odbc_fetch_row($result))
  {
    echo "<tr>";
    for($i=1;$i<=odbc_num_fields($result);$i++)
    {
      echo "<td>";
      echo odbc_result($result,$i);
      echo "</td>";
    }
    echo "</tr>";
  }

  echo "</td> </tr>";
  echo "</table >";

  //close the connection
  odbc_close ($conn);
}
else echo "odbc not connected";
?>
User avatar
Web Guru
 
Posts: 94
Joined: March 24th, 2008, 7:59 am
Location: Lahore, Pakistan


Return to Board index

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests