SlideShare a Scribd company logo
1 of 15
Chapter-3
PHP DATABASE
1
Introduction
 PHP supports wide range of relational databases, that
is why it becomes popular. Ex. Microsoft SQL Server,
MySQL, and Oracle etc.
 Advantages of relational database
 Read/write data
 Store more data
 Better organized data
 Faster access to data
 Easier to manipulate
 Relate data to other data
 Here we are going to use MySQL database
 Open source, free, easy to use, popular and good
introduction to DB concepts
2
Database:
 It is a set of tables. We should have 1 database for 1
application.
 Tables: is a set of rows and columns. It represents a
single concept such as products, customers, orders
etc. We can create relationships among tables.
 Columns: a set of data of single data type. Ex.
FirstName, LastName, Email, Password etc. columns
have types such as strings, integers, float, date etc.
 Rows: single record of data. Ex. “Abebe”, “Kebede”,
“abe@gmail.com”, “password”
 Field: is the intersection of a row and a column. Ex.
FirstName: ”Abebe”
 Index: data structure on a table to increase look up
speed.
 Foreign key: table columns whose values references
rows in another table. The foundation of relational
3
Basic database operations:
 DDL
 CREATE, DROP, ALTER, RENAME
 DML
 INSERT INTO, UPDATE, DELETE
 QUERY
 SELECT
Connecting to the MySQL Server
 PHP provides us different APIs to deal with databases
 MySQL: Original MySQL API(deprecated @v5.2,
removed @v7)
 Mysqli: MySQL improved API
 PDO: PHP Data Objects
4
 PHP database APIs
 PHP database interactions in five steps:
 Create a database connection
 Perform Database query
 Use returned data if any
 Release returned data
 Close database connection
5
 Functions
mysql_connect() or mysqli_connect()
mysql_select_db() or mysqli_select_db()
mysql_query() or mysqli_query()
mysql_fetch_rows() or mysql_fetch_array()
mysqli_fetch_assoc()
mysql_free_results()
mysql_error() or mysqli_error()
mysql_num_rows() or mysqli_affected_rows()
 Connecting to databases
 Before we do anything in MySQL using php, we should
first connect to MySQL server.
 To connect use mysqli_connect(); function
Syntax:
$con=mysqli_connect(“servername”,”dbusername”,
“dbpassword”);
echo “Success fully connected!”
6
 Creating database:
mysqli_query($con,"create database sims") ;
echo "<br>Database Successfully created";
 Selecting Database
mysqli_select_db($con,”databasename”);
 Creating a table using mysqli
$con= mysqli_connect(“localhost”,”root”,””);
mysqli_select_db($con,“databasename”) ;
mysqli_query($con,"create table feedback
(id int auto_increment,
fname varchar(20),
email varchar(20),
comment LONGTEXT, primary key(id))“
) ;
echo "Feedback table created";
7
 You can also create table this way
$connection=mysqli_connect(“localhost”,”root”,””,”sims”) ;
$sql=“create table feedback
(id int auto_increment,
fname varchar(20),
email varchar(20),
comment LONGTEXT,primary key(id))“;
if(mysqli_query($connection,$sql))
{
echo "Feedback table created";
}else
{
die(“Table not created:”.mysql_errorr());
}
8
 Creating table using mysql
$connection=mysqli_connect(“localhost”,”root”,””);
mysqli_select_db(”sims”) ;
$sql=“create table feedback
(id int auto_increment,
fname varchar(20),
email varchar(20),
comment LONGTEXT,primary key(id))“;
if(mysql_query($sql,$connection))
{
echo "Feedback table created";
}else
{
die(“Table not created:”.mysql_errorr());
}
9
 To insert data into a feed back table using mysql
<?php
$connection=mysqli_connect("localhost","root","");
mysqli_select_db("sims")or die("Database not
selected:".mysql_error());
$sql="insert into feedback(fname,email,comment,date) values
(‘gere',‘gere@gmail.com','Well Done','2017-04-27')";
if(mysqli_query($connection, $sql))
{
echo "Feedback inserted";
}else
{
die(“Failed to insert feedback:".mysql_error());
}
?>
10
Inserting data using mysqli
<?php
$connection=mysqli_connect("localhost","root","","sims
");
if($connection){
$sql="insert into feedback(fname,email,comment,date)
values (‘gere',‘gere@gmail.com','Well Done','2017-04-
27')";
if(mysqli_query($connection,$sql)){
echo "Feedback inserted";
}else
{ die(“Failed to insert
feedback:".mysqli_error($connection));}
}else{
die(“Connection failed:".mysqli_error($connection));
11
 To retrieve data from feedback table using mysql
$connection=mysqli_connect("localhost","root","");
mysqli_select_db("sims")or die("Database not selected:".mysql_error());
$sql="select * from feedback order by date desc";
$result=mysqli_query($sql,$connection);
if($result)
{
echo "<table
border=1><tr><th>Name</th><th>Email</th><th>Comment</th></tr>";
while($row=mysqli_fetch_array($result))
{
echo
"<tr><td>".$row['fname']."</td><td>".$row['email']."</td><td>".$row['comment']."<
/d></tr>";
}
echo "</table>";
}else
{
die("Record not found:".mysql_error());
}
12
 You can retrieve records this way (using mysqli)
$connection=mysqli_connect("localhost","root","","sims");
$sql="select * from feedback order by date desc";
$result=mysqli_query($connection,$sql);
if($result)
{
echo "<table
border=1><tr><th>Name</th><th>Email</th><th>Comment</th></tr>";
while($row=mysqli_fetch_assoc($result))
{
echo
"<tr><td>".$row['fname']."</td><td>".$row['email']."</td><td>".$row['comment']."<
/d></tr>";
}
echo "</tr></table>";
}else
{
die("Record not found:".mysqli_error($connection));
}
13
 To update data use the same code like insert but you
should change the sql statament
 To delete data retrieve the record you want to delete
using select statement and change the sql statement
Database Security
Sensitive information must be stored in a database in
encrypted format
Encryption in PHP
 Md5($password)
 Sha1($password)
 hash(‘sha1/md5’,$password)
 Crypt($password, $salt)
14
 SQL Injection
$string=“Insert into feedback values
(‘10’,gere’,’gere@gmail.com’,’Today’s post is
good’,’2017-04-27’)”;
Use escaping string
 Today’s or
 addslashes($string);
 mysqli_real_escape_string($connection,$string)
15

More Related Content

Similar to Chapter 3.1.pptx (20)

4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
 
Mysql
MysqlMysql
Mysql
 
MySQL with PHP
MySQL with PHPMySQL with PHP
MySQL with PHP
 
Sql
SqlSql
Sql
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
 
MYSQL-Database
MYSQL-DatabaseMYSQL-Database
MYSQL-Database
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
My sql1
My sql1My sql1
My sql1
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
PHP and MySQL.pptx
PHP and MySQL.pptxPHP and MySQL.pptx
PHP and MySQL.pptx
 
Interfacing python to mysql (11363255151).pptx
Interfacing python to mysql (11363255151).pptxInterfacing python to mysql (11363255151).pptx
Interfacing python to mysql (11363255151).pptx
 
Access
AccessAccess
Access
 
My_sql_with_php
My_sql_with_phpMy_sql_with_php
My_sql_with_php
 
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
FYBSC IT Web Programming Unit V  Advanced PHP and MySQLFYBSC IT Web Programming Unit V  Advanced PHP and MySQL
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
 
Migrating from PHP 4 to PHP 5
Migrating from PHP 4 to PHP 5Migrating from PHP 4 to PHP 5
Migrating from PHP 4 to PHP 5
 
PHP with MySQL
PHP with MySQLPHP with MySQL
PHP with MySQL
 
Database presentation
Database presentationDatabase presentation
Database presentation
 
Synapse india reviews on php and sql
Synapse india reviews on php and sqlSynapse india reviews on php and sql
Synapse india reviews on php and sql
 
Php verses my sql
Php verses my sqlPhp verses my sql
Php verses my sql
 
Php mysql connectivity
Php mysql connectivityPhp mysql connectivity
Php mysql connectivity
 

Recently uploaded

MASONRY -Building Technology and Construction
MASONRY -Building Technology and ConstructionMASONRY -Building Technology and Construction
MASONRY -Building Technology and Constructionmbermudez3
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
SCRIP Lua HTTP PROGRACMACION PLC  WECON CASCRIP Lua HTTP PROGRACMACION PLC  WECON CA
SCRIP Lua HTTP PROGRACMACION PLC WECON CANestorGamez6
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...home
 
A level Digipak development Presentation
A level Digipak development PresentationA level Digipak development Presentation
A level Digipak development Presentationamedia6
 
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`dajasot375
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricksabhishekparmar618
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...Suhani Kapoor
 
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightDelhi Call girls
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpmainac1
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfParomita Roy
 
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...Suhani Kapoor
 
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130Suhani Kapoor
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxmirandajeremy200221
 
Design Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryDesign Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryWilliamVickery6
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Delhi Call girls
 
call girls in Harsh Vihar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Harsh Vihar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Harsh Vihar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Harsh Vihar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Yantram Animation Studio Corporation
 

Recently uploaded (20)

MASONRY -Building Technology and Construction
MASONRY -Building Technology and ConstructionMASONRY -Building Technology and Construction
MASONRY -Building Technology and Construction
 
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Okhla Delhi 💯Call Us 🔝8264348440🔝
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
SCRIP Lua HTTP PROGRACMACION PLC  WECON CASCRIP Lua HTTP PROGRACMACION PLC  WECON CA
SCRIP Lua HTTP PROGRACMACION PLC WECON CA
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
 
A level Digipak development Presentation
A level Digipak development PresentationA level Digipak development Presentation
A level Digipak development Presentation
 
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
Abu Dhabi Call Girls O58993O4O2 Call Girls in Abu Dhabi`
 
Cosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable BricksCosumer Willingness to Pay for Sustainable Bricks
Cosumer Willingness to Pay for Sustainable Bricks
 
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
VIP College Call Girls Gorakhpur Bhavna 8250192130 Independent Escort Service...
 
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 nightCheap Rate Call girls Kalkaji 9205541914 shot 1500 night
Cheap Rate Call girls Kalkaji 9205541914 shot 1500 night
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
 
Kindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUpKindergarten Assessment Questions Via LessonUp
Kindergarten Assessment Questions Via LessonUp
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
VIP Russian Call Girls in Gorakhpur Deepika 8250192130 Independent Escort Ser...
 
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptx
 
Design Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William VickeryDesign Portfolio - 2024 - William Vickery
Design Portfolio - 2024 - William Vickery
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 
call girls in Harsh Vihar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Harsh Vihar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Harsh Vihar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Harsh Vihar (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
Captivating Charm: Exploring Marseille's Hillside Villas with Our 3D Architec...
 

Chapter 3.1.pptx

  • 2. Introduction  PHP supports wide range of relational databases, that is why it becomes popular. Ex. Microsoft SQL Server, MySQL, and Oracle etc.  Advantages of relational database  Read/write data  Store more data  Better organized data  Faster access to data  Easier to manipulate  Relate data to other data  Here we are going to use MySQL database  Open source, free, easy to use, popular and good introduction to DB concepts 2
  • 3. Database:  It is a set of tables. We should have 1 database for 1 application.  Tables: is a set of rows and columns. It represents a single concept such as products, customers, orders etc. We can create relationships among tables.  Columns: a set of data of single data type. Ex. FirstName, LastName, Email, Password etc. columns have types such as strings, integers, float, date etc.  Rows: single record of data. Ex. “Abebe”, “Kebede”, “abe@gmail.com”, “password”  Field: is the intersection of a row and a column. Ex. FirstName: ”Abebe”  Index: data structure on a table to increase look up speed.  Foreign key: table columns whose values references rows in another table. The foundation of relational 3
  • 4. Basic database operations:  DDL  CREATE, DROP, ALTER, RENAME  DML  INSERT INTO, UPDATE, DELETE  QUERY  SELECT Connecting to the MySQL Server  PHP provides us different APIs to deal with databases  MySQL: Original MySQL API(deprecated @v5.2, removed @v7)  Mysqli: MySQL improved API  PDO: PHP Data Objects 4
  • 5.  PHP database APIs  PHP database interactions in five steps:  Create a database connection  Perform Database query  Use returned data if any  Release returned data  Close database connection 5
  • 6.  Functions mysql_connect() or mysqli_connect() mysql_select_db() or mysqli_select_db() mysql_query() or mysqli_query() mysql_fetch_rows() or mysql_fetch_array() mysqli_fetch_assoc() mysql_free_results() mysql_error() or mysqli_error() mysql_num_rows() or mysqli_affected_rows()  Connecting to databases  Before we do anything in MySQL using php, we should first connect to MySQL server.  To connect use mysqli_connect(); function Syntax: $con=mysqli_connect(“servername”,”dbusername”, “dbpassword”); echo “Success fully connected!” 6
  • 7.  Creating database: mysqli_query($con,"create database sims") ; echo "<br>Database Successfully created";  Selecting Database mysqli_select_db($con,”databasename”);  Creating a table using mysqli $con= mysqli_connect(“localhost”,”root”,””); mysqli_select_db($con,“databasename”) ; mysqli_query($con,"create table feedback (id int auto_increment, fname varchar(20), email varchar(20), comment LONGTEXT, primary key(id))“ ) ; echo "Feedback table created"; 7
  • 8.  You can also create table this way $connection=mysqli_connect(“localhost”,”root”,””,”sims”) ; $sql=“create table feedback (id int auto_increment, fname varchar(20), email varchar(20), comment LONGTEXT,primary key(id))“; if(mysqli_query($connection,$sql)) { echo "Feedback table created"; }else { die(“Table not created:”.mysql_errorr()); } 8
  • 9.  Creating table using mysql $connection=mysqli_connect(“localhost”,”root”,””); mysqli_select_db(”sims”) ; $sql=“create table feedback (id int auto_increment, fname varchar(20), email varchar(20), comment LONGTEXT,primary key(id))“; if(mysql_query($sql,$connection)) { echo "Feedback table created"; }else { die(“Table not created:”.mysql_errorr()); } 9
  • 10.  To insert data into a feed back table using mysql <?php $connection=mysqli_connect("localhost","root",""); mysqli_select_db("sims")or die("Database not selected:".mysql_error()); $sql="insert into feedback(fname,email,comment,date) values (‘gere',‘gere@gmail.com','Well Done','2017-04-27')"; if(mysqli_query($connection, $sql)) { echo "Feedback inserted"; }else { die(“Failed to insert feedback:".mysql_error()); } ?> 10
  • 11. Inserting data using mysqli <?php $connection=mysqli_connect("localhost","root","","sims "); if($connection){ $sql="insert into feedback(fname,email,comment,date) values (‘gere',‘gere@gmail.com','Well Done','2017-04- 27')"; if(mysqli_query($connection,$sql)){ echo "Feedback inserted"; }else { die(“Failed to insert feedback:".mysqli_error($connection));} }else{ die(“Connection failed:".mysqli_error($connection)); 11
  • 12.  To retrieve data from feedback table using mysql $connection=mysqli_connect("localhost","root",""); mysqli_select_db("sims")or die("Database not selected:".mysql_error()); $sql="select * from feedback order by date desc"; $result=mysqli_query($sql,$connection); if($result) { echo "<table border=1><tr><th>Name</th><th>Email</th><th>Comment</th></tr>"; while($row=mysqli_fetch_array($result)) { echo "<tr><td>".$row['fname']."</td><td>".$row['email']."</td><td>".$row['comment']."< /d></tr>"; } echo "</table>"; }else { die("Record not found:".mysql_error()); } 12
  • 13.  You can retrieve records this way (using mysqli) $connection=mysqli_connect("localhost","root","","sims"); $sql="select * from feedback order by date desc"; $result=mysqli_query($connection,$sql); if($result) { echo "<table border=1><tr><th>Name</th><th>Email</th><th>Comment</th></tr>"; while($row=mysqli_fetch_assoc($result)) { echo "<tr><td>".$row['fname']."</td><td>".$row['email']."</td><td>".$row['comment']."< /d></tr>"; } echo "</tr></table>"; }else { die("Record not found:".mysqli_error($connection)); } 13
  • 14.  To update data use the same code like insert but you should change the sql statament  To delete data retrieve the record you want to delete using select statement and change the sql statement Database Security Sensitive information must be stored in a database in encrypted format Encryption in PHP  Md5($password)  Sha1($password)  hash(‘sha1/md5’,$password)  Crypt($password, $salt) 14
  • 15.  SQL Injection $string=“Insert into feedback values (‘10’,gere’,’gere@gmail.com’,’Today’s post is good’,’2017-04-27’)”; Use escaping string  Today’s or  addslashes($string);  mysqli_real_escape_string($connection,$string) 15