Database Connectivity in Php

This blog is going to explain how to connect database with php and display all the records of table.
STEP 1: Create a table and insert some records in it.

create table emp (id int, name varchar(50), salary int, phone varchar(20));
insert into emp values (1,’Albert’,56000,’98587485768′);
insert into emp values (2,’Robart’,58000,’98587485768′);
insert into emp values (3,’Sam’,45000,’98587485768′);

STEP 2: Write a code for database connectivity and display records in Php.

<?php
$server = “localhost”;
$user = “root”;
$password =””;
$database =”test”;
$con = @mysql_connect($server,$user,$password);
mysql_select_db($database,$con);
$query = “select * from emp”;
$data = mysql_query($query);
while($rec = mysql_fetch_array($data)) {
echo “<br/>Name : “.$rec[‘name’];
echo “</br>Salary : “.$rec[‘salary’];
echo “</br>Phone : “.$rec[‘phone’];
echo “<hr>”;
}
?>

Output :
db

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.