SlideShare a Scribd company logo
Working with PHP and MySQL
How does it work ?
• Connecting to Database,
• Creating,
• Selecting,
• Deleting,
• Updating Records in a table,
• Inserting Multiple
Database operations
Connecting to Database
<?php
$dbserver = "localhost";
$dbuser = "root";
$dbpassword = "";
$con = mysqli_connect($dbserver, $dbuser, $dbpassword);
if (!$con) {
die("Connection Error: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
<?php
$dbserver = "localhost";
$dbuser = "root";
$dbpassword = "";
$con = mysqli_connect($dbserver, $dbuser, $dbpassword);
if (!$con) {
die("Connection Error: " . mysqli_connect_error());
}
$sql = "CREATE DATABASE CollegeDb";
if (mysqli_query($con, $sql)) {
echo "Database created successfully";
} else {
echo "Error :" . mysqli_error($con);
}
mysqli_close($con);
?>
Create Table
$dbserver = "localhost";
$dbuser = "root";
$dbpassword = "";
$database = “collegeDb”
$con = mysqli_connect($dbserver, $dbuser, $dbpassword, $database);
if (!$con) {
die("Connection Error: " . mysqli_connect_error());
}
$sql = "CREATE TABLE Students (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
address VARCHAR(50)
)";
if (mysqli_query($con, $sql)) {
echo "Students table created successfully";
} else {
echo "Error creating table: " . mysqli_error($con);
}
mysqli_close($con);
Insert
$fname = “John”;
$lname = “Nash”;
$address = “Kathmandu”;
$sql = "INSERT INTO students (firstname, lastname, address)
VALUES ('$fname', '$lname', '$address')";
if (mysqli_query($con, $sql)) {
echo "New student added successfully";
} else {
echo mysqli_error($conn);
}
mysqli_close($conn);
?>
Select/Display
$sql = "SELECT id, firstname, lastname FROM students";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($con);
Delete
$sql = "delete from students where id = 2";
if (mysqli_query($con, $sql)) {
echo "Student deleted successfully";
} else {
echo mysqli_error($conn);
}
mysqli_close($conn);
?>
update
$sql = "update students set firstname='nohj' where id=2";
if (mysqli_query($con, $sql)) {
echo "Student deleted successfully";
} else {
echo mysqli_error($con);
}
mysqli_close($con);
Insert Multiple
$sql = "INSERT INTO Students (firstname, lastname, address)
VALUES ('John', 'Nash', 'john@abc.com');";
$sql .= "INSERT INTO Students (firstname, lastname, address)
VALUES ('Kamal', 'KC', 'mary@abc.com');";
$sql .= "INSERT INTO Students (firstname, lastname, address)
VALUES ('Ruby', 'Sth', 'julie@abc.com')";
if (mysqli_multi_query($con, $sql)) {
echo "Multiple records inserted successfully";
} else {
echo "Error: " . mysqli_error($con);
}
mysqli_close($con);

More Related Content

Similar to Basics of Working with PHP and MySQL.pptx (20)

DOCX
Php.docx
CHMuhammadZohaibZafa
 
PPTX
Practical MySQL.pptx
HussainUsman4
 
KEY
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Masahiro Nagano
 
PPTX
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Arti Parab Academics
 
PPTX
Connecting_to_Database(MySQL)_in_PHP.pptx
TempMail233488
 
PDF
与 PHP 和 Perl 使用 MySQL 数据库
YUCHENG HU
 
PDF
Your code sucks, let's fix it - DPC UnCon
Rafael Dohms
 
PDF
Mysql & Php
Inbal Geffen
 
PPTX
chapter_Seven Database manipulation using php.pptx
Getawu
 
PDF
Practica csv
Nones Pomposo
 
KEY
Introduction à CoffeeScript pour ParisRB
jhchabran
 
PPTX
lecture 7 - Introduction to MySQL with PHP.pptx
AOmaAli
 
PPTX
Laravel
Sayed Ahmed
 
PPTX
Tidy Up Your Code
Abbas Ali
 
PPTX
Presentation1
Rahadyan Gusti
 
PPTX
Drupal 8 database api
Viswanath Polaki
 
TXT
Daily notes
meghendra168
 
PDF
You code sucks, let's fix it
Rafael Dohms
 
PDF
veracruz
tutorialsruby
 
Practical MySQL.pptx
HussainUsman4
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Masahiro Nagano
 
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Arti Parab Academics
 
Connecting_to_Database(MySQL)_in_PHP.pptx
TempMail233488
 
与 PHP 和 Perl 使用 MySQL 数据库
YUCHENG HU
 
Your code sucks, let's fix it - DPC UnCon
Rafael Dohms
 
Mysql & Php
Inbal Geffen
 
chapter_Seven Database manipulation using php.pptx
Getawu
 
Practica csv
Nones Pomposo
 
Introduction à CoffeeScript pour ParisRB
jhchabran
 
lecture 7 - Introduction to MySQL with PHP.pptx
AOmaAli
 
Laravel
Sayed Ahmed
 
Tidy Up Your Code
Abbas Ali
 
Presentation1
Rahadyan Gusti
 
Drupal 8 database api
Viswanath Polaki
 
Daily notes
meghendra168
 
You code sucks, let's fix it
Rafael Dohms
 
veracruz
tutorialsruby
 

Recently uploaded (20)

PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Introduction to Basic Renewable Energy.pptx
examcoordinatormesu
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Big Data and Data Science hype .pptx
SUNEEL37
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Introduction to Basic Renewable Energy.pptx
examcoordinatormesu
 
Design Thinking basics for Engineers.pdf
CMR University
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Big Data and Data Science hype .pptx
SUNEEL37
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Ad

Basics of Working with PHP and MySQL.pptx

  • 1. Working with PHP and MySQL
  • 2. How does it work ?
  • 3. • Connecting to Database, • Creating, • Selecting, • Deleting, • Updating Records in a table, • Inserting Multiple Database operations
  • 4. Connecting to Database <?php $dbserver = "localhost"; $dbuser = "root"; $dbpassword = ""; $con = mysqli_connect($dbserver, $dbuser, $dbpassword); if (!$con) { die("Connection Error: " . mysqli_connect_error()); } echo "Connected successfully"; ?>
  • 5. <?php $dbserver = "localhost"; $dbuser = "root"; $dbpassword = ""; $con = mysqli_connect($dbserver, $dbuser, $dbpassword); if (!$con) { die("Connection Error: " . mysqli_connect_error()); } $sql = "CREATE DATABASE CollegeDb"; if (mysqli_query($con, $sql)) { echo "Database created successfully"; } else { echo "Error :" . mysqli_error($con); } mysqli_close($con); ?>
  • 6. Create Table $dbserver = "localhost"; $dbuser = "root"; $dbpassword = ""; $database = “collegeDb” $con = mysqli_connect($dbserver, $dbuser, $dbpassword, $database); if (!$con) { die("Connection Error: " . mysqli_connect_error()); } $sql = "CREATE TABLE Students ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, address VARCHAR(50) )"; if (mysqli_query($con, $sql)) { echo "Students table created successfully"; } else { echo "Error creating table: " . mysqli_error($con); } mysqli_close($con);
  • 7. Insert $fname = “John”; $lname = “Nash”; $address = “Kathmandu”; $sql = "INSERT INTO students (firstname, lastname, address) VALUES ('$fname', '$lname', '$address')"; if (mysqli_query($con, $sql)) { echo "New student added successfully"; } else { echo mysqli_error($conn); } mysqli_close($conn); ?>
  • 8. Select/Display $sql = "SELECT id, firstname, lastname FROM students"; $result = mysqli_query($con, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo $row["firstname"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } mysqli_close($con);
  • 9. Delete $sql = "delete from students where id = 2"; if (mysqli_query($con, $sql)) { echo "Student deleted successfully"; } else { echo mysqli_error($conn); } mysqli_close($conn); ?>
  • 10. update $sql = "update students set firstname='nohj' where id=2"; if (mysqli_query($con, $sql)) { echo "Student deleted successfully"; } else { echo mysqli_error($con); } mysqli_close($con);
  • 11. Insert Multiple $sql = "INSERT INTO Students (firstname, lastname, address) VALUES ('John', 'Nash', '[email protected]');"; $sql .= "INSERT INTO Students (firstname, lastname, address) VALUES ('Kamal', 'KC', '[email protected]');"; $sql .= "INSERT INTO Students (firstname, lastname, address) VALUES ('Ruby', 'Sth', '[email protected]')"; if (mysqli_multi_query($con, $sql)) { echo "Multiple records inserted successfully"; } else { echo "Error: " . mysqli_error($con); } mysqli_close($con);