Database Connectivity Using PHP
Once we have created the database and tables in MYSQL,
the task left is to access and manipulated the data stored in
the tables.
Steps in DatabaseConnectivity:
1.Connect to the server
,
First of all we have to connect to the server MYSQL,
using the mysql connect() command or function
The general form is,
mysql connect("severname",username,password);
(a) servername
It is used to specify the name of the server.
In our case it is "localhost"
(b) username
It is used to specify the name of the user
having access to the database .In our case it is root. +
(c) password
It is used to specify the password given with
the username in our case it is blank.
e.g.
$con =
mysql connect ("localhost", "root", ""):
The $con will be treated as the connection object.
2. Select the database
Next you have to select the database on which you want
to manipulate the various operations.
For this purpose we have to make use of the
mysql select db);
The general form is,
mysql select db("databasename",connectionobject);
e.g.
mysql select db("my db", Scon);
3. Specify the query
In this step you have to specify the sql statement on
the basis of the operation you want to
perform.
For this purpose you have to make use of
mysql query() function
The general form is
,
mysql query("sql query");
(a) Reteriving Record from the table
In thiscase you have to make used of SELECT
statement in the sql query.
e.g.
$result = mysql query ("SELECT
FROM person");
Sresult willbe considered as a recordset,
which will store all the records
returned by the SQl select query.
Now we have to access the records row wise or
one record at a time.
e.g.
$row =
mysql fetch array ($result)
mysql fecth array) will access one row from
the recordset at a time
Srow willcontain the entire record.
Now to access the particular field value
Srow["fieldname"];
e.g.
echo $row['FirstName']
$row['LastName'] ;
QList all records from employee table in the ems
database.
<html>
<head>
<title>Using Database Connectivity</title>
</head>
<body bgeolor-cyan>
<?php
l/connect to the server
$con = mysql connect("localhost",","root", "");
l/select the database
mysql select db("ems", $con);
llexecute the query
Sresult=mysql _query("select * from employee");
I/list allrecords
while(Srow-mysql_fetch_array(Sresult))
echo "Employee ld :".Srow["empid"],"<br>";
echo "Employee Name:".
Srow["ename"]."<br>";
echo "Employee Salary :".
Srow["esalary"]."<br><br>";
?>
<body>
</html>