SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Similaire à 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
 

Dernier

Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...
Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...
Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...Nitya salvi
 
Pondicherry Escorts Service Girl ^ 9332606886, WhatsApp Anytime Pondicherry
Pondicherry Escorts Service Girl ^ 9332606886, WhatsApp Anytime PondicherryPondicherry Escorts Service Girl ^ 9332606886, WhatsApp Anytime Pondicherry
Pondicherry Escorts Service Girl ^ 9332606886, WhatsApp Anytime Pondicherrymeghakumariji156
 
How to Build a Simple Shopify Website
How to Build a Simple Shopify WebsiteHow to Build a Simple Shopify Website
How to Build a Simple Shopify Websitemark11275
 
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...gajnagarg
 
Q4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentationQ4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentationZenSeloveres
 
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdfJordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdfamanda2495
 
LANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEK
LANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEKLANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEK
LANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEKMarekMitek1
 
Abortion pills in Kuwait 🚚+966505195917 but home delivery available in Kuwait...
Abortion pills in Kuwait 🚚+966505195917 but home delivery available in Kuwait...Abortion pills in Kuwait 🚚+966505195917 but home delivery available in Kuwait...
Abortion pills in Kuwait 🚚+966505195917 but home delivery available in Kuwait...drmarathore
 
怎样办理伦敦国王学院毕业证(KCL毕业证书)成绩单留信认证
怎样办理伦敦国王学院毕业证(KCL毕业证书)成绩单留信认证怎样办理伦敦国王学院毕业证(KCL毕业证书)成绩单留信认证
怎样办理伦敦国王学院毕业证(KCL毕业证书)成绩单留信认证eeanqy
 
Furniture & Joinery Details_Designs.pptx
Furniture & Joinery Details_Designs.pptxFurniture & Joinery Details_Designs.pptx
Furniture & Joinery Details_Designs.pptxNikhil Raut
 
Gamestore case study UI UX by Amgad Ibrahim
Gamestore case study UI UX by Amgad IbrahimGamestore case study UI UX by Amgad Ibrahim
Gamestore case study UI UX by Amgad Ibrahimamgadibrahim92
 
Minimalist Orange Portfolio by Slidesgo.pptx
Minimalist Orange Portfolio by Slidesgo.pptxMinimalist Orange Portfolio by Slidesgo.pptx
Minimalist Orange Portfolio by Slidesgo.pptxbalqisyamutia
 
High Profile Escorts Nerul WhatsApp +91-9930687706, Best Service
High Profile Escorts Nerul WhatsApp +91-9930687706, Best ServiceHigh Profile Escorts Nerul WhatsApp +91-9930687706, Best Service
High Profile Escorts Nerul WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Independent Escorts Goregaon WhatsApp +91-9930687706, Best Service
Independent Escorts Goregaon WhatsApp +91-9930687706, Best ServiceIndependent Escorts Goregaon WhatsApp +91-9930687706, Best Service
Independent Escorts Goregaon WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...Nitya salvi
 
Top profile Call Girls In fatehgarh [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In fatehgarh [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In fatehgarh [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In fatehgarh [ 7014168258 ] Call Me For Genuine Models...gajnagarg
 
一比一定(购)滑铁卢大学毕业证(UW毕业证)成绩单学位证
一比一定(购)滑铁卢大学毕业证(UW毕业证)成绩单学位证一比一定(购)滑铁卢大学毕业证(UW毕业证)成绩单学位证
一比一定(购)滑铁卢大学毕业证(UW毕业证)成绩单学位证wpkuukw
 
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证eeanqy
 
一比一原版(ANU毕业证书)澳大利亚国立大学毕业证原件一模一样
一比一原版(ANU毕业证书)澳大利亚国立大学毕业证原件一模一样一比一原版(ANU毕业证书)澳大利亚国立大学毕业证原件一模一样
一比一原版(ANU毕业证书)澳大利亚国立大学毕业证原件一模一样yhavx
 
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...ZurliaSoop
 

Dernier (20)

Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...
Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...
Just Call Vip call girls Kasganj Escorts ☎️8617370543 Two shot with one girl ...
 
Pondicherry Escorts Service Girl ^ 9332606886, WhatsApp Anytime Pondicherry
Pondicherry Escorts Service Girl ^ 9332606886, WhatsApp Anytime PondicherryPondicherry Escorts Service Girl ^ 9332606886, WhatsApp Anytime Pondicherry
Pondicherry Escorts Service Girl ^ 9332606886, WhatsApp Anytime Pondicherry
 
How to Build a Simple Shopify Website
How to Build a Simple Shopify WebsiteHow to Build a Simple Shopify Website
How to Build a Simple Shopify Website
 
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In eluru [ 7014168258 ] Call Me For Genuine Models We ...
 
Q4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentationQ4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentation
 
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdfJordan_Amanda_DMBS202404_PB1_2024-04.pdf
Jordan_Amanda_DMBS202404_PB1_2024-04.pdf
 
LANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEK
LANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEKLANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEK
LANDSCAPE ARCHITECTURE PORTFOLIO - MAREK MITACEK
 
Abortion pills in Kuwait 🚚+966505195917 but home delivery available in Kuwait...
Abortion pills in Kuwait 🚚+966505195917 but home delivery available in Kuwait...Abortion pills in Kuwait 🚚+966505195917 but home delivery available in Kuwait...
Abortion pills in Kuwait 🚚+966505195917 but home delivery available in Kuwait...
 
怎样办理伦敦国王学院毕业证(KCL毕业证书)成绩单留信认证
怎样办理伦敦国王学院毕业证(KCL毕业证书)成绩单留信认证怎样办理伦敦国王学院毕业证(KCL毕业证书)成绩单留信认证
怎样办理伦敦国王学院毕业证(KCL毕业证书)成绩单留信认证
 
Furniture & Joinery Details_Designs.pptx
Furniture & Joinery Details_Designs.pptxFurniture & Joinery Details_Designs.pptx
Furniture & Joinery Details_Designs.pptx
 
Gamestore case study UI UX by Amgad Ibrahim
Gamestore case study UI UX by Amgad IbrahimGamestore case study UI UX by Amgad Ibrahim
Gamestore case study UI UX by Amgad Ibrahim
 
Minimalist Orange Portfolio by Slidesgo.pptx
Minimalist Orange Portfolio by Slidesgo.pptxMinimalist Orange Portfolio by Slidesgo.pptx
Minimalist Orange Portfolio by Slidesgo.pptx
 
High Profile Escorts Nerul WhatsApp +91-9930687706, Best Service
High Profile Escorts Nerul WhatsApp +91-9930687706, Best ServiceHigh Profile Escorts Nerul WhatsApp +91-9930687706, Best Service
High Profile Escorts Nerul WhatsApp +91-9930687706, Best Service
 
Independent Escorts Goregaon WhatsApp +91-9930687706, Best Service
Independent Escorts Goregaon WhatsApp +91-9930687706, Best ServiceIndependent Escorts Goregaon WhatsApp +91-9930687706, Best Service
Independent Escorts Goregaon WhatsApp +91-9930687706, Best Service
 
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
Call Girls In Ratnagiri Escorts ☎️8617370543 🔝 💃 Enjoy 24/7 Escort Service En...
 
Top profile Call Girls In fatehgarh [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In fatehgarh [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In fatehgarh [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In fatehgarh [ 7014168258 ] Call Me For Genuine Models...
 
一比一定(购)滑铁卢大学毕业证(UW毕业证)成绩单学位证
一比一定(购)滑铁卢大学毕业证(UW毕业证)成绩单学位证一比一定(购)滑铁卢大学毕业证(UW毕业证)成绩单学位证
一比一定(购)滑铁卢大学毕业证(UW毕业证)成绩单学位证
 
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
怎样办理伯明翰大学学院毕业证(Birmingham毕业证书)成绩单留信认证
 
一比一原版(ANU毕业证书)澳大利亚国立大学毕业证原件一模一样
一比一原版(ANU毕业证书)澳大利亚国立大学毕业证原件一模一样一比一原版(ANU毕业证书)澳大利亚国立大学毕业证原件一模一样
一比一原版(ANU毕业证书)澳大利亚国立大学毕业证原件一模一样
 
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
Jual Obat Aborsi Bandung ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan ...
 

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