Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

UNIT-V : Database Operations

============================
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.

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-V : Database Operations

==============================
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()

Databse Connection Code:


=============================
<?php
$conn=mysqli_connect("localhost","vjtech","vjtech2013","revis
ionphpdb");
if($conn)

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-V : Database Operations

{
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

echo "Table created successfully!!!";


}
else
{
echo "Table not created!!!";
}
}
else
{
echo "Unable to connect";
}
mysqli_close($conn);
?>

===============
Insert Records
===============
<?php
$conn=mysqli_connect("localhost","vjtech","vjtech2013","revis
ionphpdb");

if($conn)
{

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-V : Database Operations

echo "Connection established successfully!!!";


$query="insert into vehicle values('9999','Kia','67000')";
if(mysqli_query($conn,$query))
{
echo "Records created successfully!!!";
}
else
{
echo "Records not created!!!";
}
}
else
{
echo "Unable to connect";
}
mysqli_close($conn);
?>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-V : Database Operations

===============
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!!!";
}

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-V : Database Operations

}
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.

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


UNIT-V : Database Operations

Inspiring Your Success

VJTech Academy…

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)

You might also like