PHP – Connect to MySQL Database

The given PHP example helps you to make a connection with MySQL Database and then get the record
form a table.

Code Example:
<?php
$hostname="localhost";
$username="root";
$password="root";
$dbname="school";

mysql_connect($hostname,$username, $password) or die ("Unable to connect to database!");
mysql_select_db($dbname) or die ("No database exist!");
$query = "SELECT firstName, lastName FROM student";
$result = mysql_query($query);
if($result){
    while($row = mysql_fetch_array($result)){
        $firstName = $row["firstName"];
		$lastName = $row["lastName"];
        echo "Name: ".$firstName. " " . $lastName;
    }
}
?>

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

1 Comment

  1. avatarAsh

    I prefer to do this way..
    very easy way to connect to database

Leave a Comment

Your email address will not be published. Required fields are marked *