Category Covered

php mysql connection

<?php
$username = "yourMysqlUserName";
$password = "yourMysqlPassword";
$hostname = "yourHostName";

if you use wamp/xxamp then default setting for user, password and host will be something like that

$username = "root";
$password = "";
$hostname = "localhost";

//Here mysql_connection is a mysql function that will connect to the database
$connectionString = mysql_connect($hostname, $username, $password)
or die("Connection Failed");
echo "Connected<br>";
?>

<?php
//Now select a database from your mysql
$selected = mysql_select_db("examples",$connectionString)
?>

if you have the phpmyadmin user interface then go there may you can use the following url if you dont change the port and anything else.

http://localhost/phpmyadmin


Now create database
you can create database using phpmyadmin GUI or use the following code and paste it to the sql holder.

CREATE DATABASE `myDb`;
USE `myDb`;
CREATE TABLE `myTable` (
`id` int UNIQUE NOT NULL,
`name` varchar(40),
`year` varchar(50),
PRIMARY KEY(id)
);
INSERT INTO myTable VALUES(1,'Mercedes','2000');
INSERT INTO myTable VALUES(2,'BMW','2004');
INSERT INTO myTable VALUES(3,'Audi','2001');

Mysql database creation complete now write code on php page to get data from myDb


<?php
//The following mysql_query function will return the data from myTable.
$result = mysql_query("SELECT id, model, year FROM myTable");
//fetching data from myDb the database
while ($row = mysql_fetch_array($result)) {
echo "ID:".$row{'id'}." Name:".$row{'model'}."
".$row{'year'}."<br>";
}
?>