Professional Documents
Culture Documents
Unit V
Unit V
============================
UNIT-V : Database Operations
============================
- Database is a collection of related data.
- We can organize large amount of data in database so that we can
easily access it.
- DBMS is a system software for creating and managing databases.
- We can create, update, delete and modify the data.
- Nowdays, we use relational databae management system(RDMBS)
for store and manage hugh amount of data.
- PHP will works with many databases like postgreSQL,sybase,oracle
and many more but most commonly used is MYSQL database.
- MYSQL database is free to use.
- PHP is very flexible with MYSQL database compared with other
databases.
- Data transfer between MYSQL and php is very smoothly and fixible.
- MYSQL is open source RDMBS.
- We can store the data in MYSQL in the form of table.
- We use PHP for web application development.
- PHP provides various functions to access MYSQL database and
manipulate the data records inside the MYSQL database.
- MYSQL handles more expensive and powerful database packages.
==============================
Retrieving the Query Result:
==============================
- We can retrieve table data in PHP code by using mysqli_query()
function. Here, we can execute select query.
- There are two other mysqli functions used in select query:
1) mysqli_num_rows(mysqli_result $result) - which returns no of
rows.
2) mysqli_fetch_assoc(mysqli_result $result) - Which return row as
an associative array. Each key of the array represents the column
name of the table. It returns NULL if there are no more rows.
important methods:
===================
mysqli_connec(serverinfo,username,password,databasename)
mysqli_query($connect,"SQL Statement");
mysqli_close()
{
echo "Connection established successfully!!!";
}
else
{
echo "Unable to connect";
}
mysqli_close($conn);
?>
=======================
Table creation
=======================
<?php
$conn=mysqli_connect("localhost","vjtech","vjtech2013","revis
ionphpdb");
if($conn)
{
echo "Connection established successfully!!!";
$query="create table vehicle(vehicle_no
int(5),vehicle_name varchar(10),vehicle_price int(5))";
if(mysqli_query($conn,$query))
{
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
UNIT-V : Database Operations
===============
Insert Records
===============
<?php
$conn=mysqli_connect("localhost","vjtech","vjtech2013","revis
ionphpdb");
if($conn)
{
===============
update records
=================
<?php
$conn=mysqli_connect("localhost","vjtech","vjtech2013","revis
ionphpdb");
if($conn)
{
echo "Connection established successfully!!!";
$query="update vehicle set vehicle_name='Suzuki' where
vehicle_no='9999'";
if(mysqli_query($conn,$query))
{
echo "Records updated successfully!!!";
}
else
{
echo "Records not updated!!!";
}
}
else
{
echo "Unable to connect";
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
UNIT-V : Database Operations
}
mysqli_close($conn);
?>
====================
delete records
=====================
<?php
$conn=mysqli_connect("localhost","vjtech","vjtech2013","revis
ionphpdb");
if($conn)
{
echo "Connection established successfully!!!";
$query="delete from vehicle where vehicle_no='9999'";
if(mysqli_query($conn,$query))
{
echo "Records deleted successfully!!!";
}
else
{
echo "Records not deleted!!!";
}
}
else
{
echo "Unable to connect";
}
mysqli_close($conn);
?>
=============================
Retrieving the query results
=============================
- Below two methods are important
1) mysqli_num_rows() which returns no of rows.
2) mysqli_fetch_assoc() which returns row as an associative array.
VJTech Academy…