Saturday, September 11, 2010

How to connect php to SQL database

<?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  FROM cars 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 connectionmssql_close($dbhandle);
?>



Here mssql_connect is the function for making connection with mysql and php we need pass the values like username, password  and databases name. If you are using XAMP as the server you need to give root as user name no need to give the password.I will tell you how to make a data base in next post.

0 comments:

Post a Comment

 

Blogroll

Site Info

Text

Free PHP Scripts Copyright © 2009 WoodMag is Designed by Ipietoon for Free Blogger Template