SlideShare una empresa de Scribd logo
1 de 19
PHP and DatabasePHP and Database
 MysqlMysql – popular open-source database– popular open-source database
management systemmanagement system
 PHPPHP usually works withusually works with MysqlMysql for web-basedfor web-based
database applicationsdatabase applications
 LAMPLAMP applications—Web-based applicationsapplications—Web-based applications
that usethat use LynuxLynux,, ApacheApache,, MysqlMysql, and, and
php/pearl/pythonphp/pearl/python
 Connect to host server which has MysqlConnect to host server which has Mysql
installedinstalled
 Select a databaseSelect a database
 Form an SQL statementForm an SQL statement
 Execute the SQL statement and (optionally)Execute the SQL statement and (optionally)
return a record setreturn a record set
 Extract data from recordset using phpExtract data from recordset using php
 Close connectionClose connection
<?php<?php
$host = ‘localhost’;$host = ‘localhost’;
$username = ‘peter’;$username = ‘peter’;
$pswd = ‘!?+&*’;$pswd = ‘!?+&*’;
$dbName = “myDB”;$dbName = “myDB”;
$con = mysql_connect($host, $username,$con = mysql_connect($host, $username,
$pswd);$pswd);
if (!$con){if (!$con){
die('Could not connect: ‘die('Could not connect: ‘
. mysql_error());. mysql_error());
}}
$db = mysql_select_db($dbName,$db = mysql_select_db($dbName,
$con) or die(mysql_error());$con) or die(mysql_error());
?>?>
 SQL
 CREATE DATABASE database_name
 PHP
$con = mysql_connect("localhost","peter",
"abc123");
$sql = “CREATE DATABASE myDB”;
mysql_query(“$sql”, $con));
 SQL
 CREATE TABLE table_name
(column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
);
 PHP
// Connect to Mysql
$con = mysql_connect(. . .);
// Create database
mysql_query("CREATE DATABASE my_db",$con);
// Select DB
mysql_select_db("my_db", $con);
// Create table
$sql = "CREATE TABLE Persons(
FirstName varchar(15),
LastName varchar(15),
Age int
)”;
// Execute SQL statement
mysql_query($sql, $con);
";
Contd:Contd:
 When DB already exists:When DB already exists:
 PHPPHP
$con = mysql_connect("localhost","peter",$con = mysql_connect("localhost","peter",
"abc123");"abc123");
$db = mysql_select_db("my_db“,$db = mysql_select_db("my_db“,
$con);$con);
 SQL
SELECT colName1, colName2, colName3SELECT colName1, colName2, colName3
FROM Persons;FROM Persons;
 PHP
$con = mysql_connect(. . .);$con = mysql_connect(. . .);
mysql_select_db("my_db“, $con);mysql_select_db("my_db“, $con);
$sql = “SELECT FirstName, LastName$sql = “SELECT FirstName, LastName
FROM Persons;”;FROM Persons;”;
$result = mysql_query($sql);$result = mysql_query($sql);
 PHP
$result = mysql_query($sql);$result = mysql_query($sql);
while($row =while($row =
mysql_fetch_array($result)){mysql_fetch_array($result)){
echo $row['FirstName'] . " " .echo $row['FirstName'] . " " .
$row['LastName'];$row['LastName'];
echo "<br />";echo "<br />";
}}
 SQL
INSERT INTO table_nameINSERT INTO table_name
VALUES (value1, value2, value3,...)VALUES (value1, value2, value3,...)
oror
INSERT INTO table_nameINSERT INTO table_name
(column1, column2,(column1, column2,
column3,...)column3,...)
VALUES (value1, value2, value3,...)VALUES (value1, value2, value3,...)
<?php
$con = mysql_connect(“…",“…",“…");
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO Persons (FirstName,
LastName, Age)
VALUES ('Peter','Griffin','35')");
mysql_query("INSERT INTO Persons (FirstName,
LastName, Age)
VALUES ('Glenn', ‘Brown', '33')");
mysql_close($con);
?>
Contd:Contd:
<html><html>
<body><body>
<form action="insert.php" method="post"><form action="insert.php" method="post">
Firstname: <input type="text" name=“nameF" />Firstname: <input type="text" name=“nameF" />
Lastname: <input type="text" name=“nameL" />Lastname: <input type="text" name=“nameL" />
Age: <input type="text" name="age" />Age: <input type="text" name="age" />
<input type="submit" /><input type="submit" />
</form></form>
</body></body>
</html></html>
<?php
$con = mysql_connect(“…",“…",“…");
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("my_db", $con);
$sql="INSERT INTO Persons
(FirstName, LastName, Age)
VALUES ('$_POST[firstname]',
'$_POST[lastname]',
'$_POST[age]')";
if (!mysql_query($sql,$con))
die('Error: ' . mysql_error());
echo "1 record added";
mysql_close($con)
?>
Contd:Contd:
 SQL
UPDATE table_nameUPDATE table_name
SET column1=value, column2=value2,...SET column1=value, column2=value2,...
WHERE some_column = some_value;WHERE some_column = some_value;
<?php
$con = mysql_connect(“…",“…",“…");
if (!$con)
die('Could not connect: ' .
mysql_error());
mysql_select_db("my_db", $con);
$sql = “UPDATE Persons
SET Age = ’36’
WHERE FirstName = ‘Peter’
AND LastName = “Griffin’”;
mysql_query($sql, $con);
mysql_close($con);
?>
Contd:Contd:
 SQL
Delete table_nameDelete table_name
WHERE some_column = some_value;WHERE some_column = some_value;
<?php
$con = mysql_connect(“…",“…",“…");
if (!$con)
die('Could not connect: ' .
mysql_error());
mysql_select_db("my_db", $con);
$sql = “DELETE FROM Persons
WHERE FirstName = ‘Peter’
AND LastName = “Griffin’”);
mysql_query($sql, $con);
mysql_close($con);
?>
Contd:Contd:

Más contenido relacionado

La actualidad más candente

Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsPierre MARTIN
 
Database Management - Lecture 4 - PHP and Mysql
Database Management - Lecture 4 - PHP and MysqlDatabase Management - Lecture 4 - PHP and Mysql
Database Management - Lecture 4 - PHP and MysqlAl-Mamun Sarkar
 
PHP - Getting good with MySQL part II
 PHP - Getting good with MySQL part II PHP - Getting good with MySQL part II
PHP - Getting good with MySQL part IIFirdaus Adib
 
Getting out of Callback Hell in PHP
Getting out of Callback Hell in PHPGetting out of Callback Hell in PHP
Getting out of Callback Hell in PHPArul Kumaran
 
OWASP TOP 10 for PHP Programmers
OWASP TOP 10 for PHP ProgrammersOWASP TOP 10 for PHP Programmers
OWASP TOP 10 for PHP Programmersrjsmelo
 
Not Really PHP by the book
Not Really PHP by the bookNot Really PHP by the book
Not Really PHP by the bookRyan Kilfedder
 
SQL Injection in PHP
SQL Injection in PHPSQL Injection in PHP
SQL Injection in PHPDave Ross
 
OWASP Top 10 at International PHP Conference 2014 in Berlin
OWASP Top 10 at International PHP Conference 2014 in BerlinOWASP Top 10 at International PHP Conference 2014 in Berlin
OWASP Top 10 at International PHP Conference 2014 in BerlinTobias Zander
 
Threading
ThreadingThreading
Threadingb290572
 
Redis the better NoSQL
Redis the better NoSQLRedis the better NoSQL
Redis the better NoSQLOpenFest team
 
PHP - PDO Objects
PHP - PDO ObjectsPHP - PDO Objects
PHP - PDO ObjectsAJINKYA N
 

La actualidad más candente (20)

Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
 
Php
PhpPhp
Php
 
Php (1)
Php (1)Php (1)
Php (1)
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 
Database Management - Lecture 4 - PHP and Mysql
Database Management - Lecture 4 - PHP and MysqlDatabase Management - Lecture 4 - PHP and Mysql
Database Management - Lecture 4 - PHP and Mysql
 
PHP - Getting good with MySQL part II
 PHP - Getting good with MySQL part II PHP - Getting good with MySQL part II
PHP - Getting good with MySQL part II
 
Getting out of Callback Hell in PHP
Getting out of Callback Hell in PHPGetting out of Callback Hell in PHP
Getting out of Callback Hell in PHP
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
 
Php talk
Php talkPhp talk
Php talk
 
OWASP TOP 10 for PHP Programmers
OWASP TOP 10 for PHP ProgrammersOWASP TOP 10 for PHP Programmers
OWASP TOP 10 for PHP Programmers
 
Not Really PHP by the book
Not Really PHP by the bookNot Really PHP by the book
Not Really PHP by the book
 
SQL Injection in PHP
SQL Injection in PHPSQL Injection in PHP
SQL Injection in PHP
 
OWASP Top 10 at International PHP Conference 2014 in Berlin
OWASP Top 10 at International PHP Conference 2014 in BerlinOWASP Top 10 at International PHP Conference 2014 in Berlin
OWASP Top 10 at International PHP Conference 2014 in Berlin
 
Threading
ThreadingThreading
Threading
 
Redis the better NoSQL
Redis the better NoSQLRedis the better NoSQL
Redis the better NoSQL
 
Php mysq
Php mysqPhp mysq
Php mysq
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
PHP - PDO Objects
PHP - PDO ObjectsPHP - PDO Objects
PHP - PDO Objects
 

Destacado

Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2Mudasir Syed
 
Cookies in php lecture 2
Cookies in php  lecture  2Cookies in php  lecture  2
Cookies in php lecture 2Mudasir Syed
 
Adminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdminAdminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdminMudasir Syed
 
PHP mysql Installing my sql 5.1
PHP mysql  Installing my sql 5.1PHP mysql  Installing my sql 5.1
PHP mysql Installing my sql 5.1Mudasir Syed
 
Time manipulation lecture 1
Time manipulation lecture 1 Time manipulation lecture 1
Time manipulation lecture 1 Mudasir Syed
 
Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php Mudasir Syed
 
Web forms and html lecture Number 5
Web forms and html lecture Number 5Web forms and html lecture Number 5
Web forms and html lecture Number 5Mudasir Syed
 
Web forms and html lecture Number 2
Web forms and html lecture Number 2Web forms and html lecture Number 2
Web forms and html lecture Number 2Mudasir Syed
 
PHP mysql Database normalizatin
PHP mysql  Database normalizatinPHP mysql  Database normalizatin
PHP mysql Database normalizatinMudasir Syed
 
PHP mysql Er diagram
PHP mysql  Er diagramPHP mysql  Er diagram
PHP mysql Er diagramMudasir Syed
 
Cookies in php lecture 1
Cookies in php lecture 1Cookies in php lecture 1
Cookies in php lecture 1Mudasir Syed
 
Reporting using FPDF
Reporting using FPDFReporting using FPDF
Reporting using FPDFMudasir Syed
 
PHP mysql Mysql joins
PHP mysql  Mysql joinsPHP mysql  Mysql joins
PHP mysql Mysql joinsMudasir Syed
 
Time manipulation lecture 2
Time manipulation lecture 2Time manipulation lecture 2
Time manipulation lecture 2Mudasir Syed
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2Mudasir Syed
 
Form validation server side
Form validation server side Form validation server side
Form validation server side Mudasir Syed
 
Form validation with built in functions
Form validation with built in functions Form validation with built in functions
Form validation with built in functions Mudasir Syed
 

Destacado (20)

Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
 
Cookies in php lecture 2
Cookies in php  lecture  2Cookies in php  lecture  2
Cookies in php lecture 2
 
Ajax
Ajax Ajax
Ajax
 
Adminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdminAdminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdmin
 
PHP mysql Installing my sql 5.1
PHP mysql  Installing my sql 5.1PHP mysql  Installing my sql 5.1
PHP mysql Installing my sql 5.1
 
Time manipulation lecture 1
Time manipulation lecture 1 Time manipulation lecture 1
Time manipulation lecture 1
 
PHP mysql Sql
PHP mysql  SqlPHP mysql  Sql
PHP mysql Sql
 
Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
 
Web forms and html lecture Number 5
Web forms and html lecture Number 5Web forms and html lecture Number 5
Web forms and html lecture Number 5
 
Web forms and html lecture Number 2
Web forms and html lecture Number 2Web forms and html lecture Number 2
Web forms and html lecture Number 2
 
PHP mysql Database normalizatin
PHP mysql  Database normalizatinPHP mysql  Database normalizatin
PHP mysql Database normalizatin
 
PHP mysql Er diagram
PHP mysql  Er diagramPHP mysql  Er diagram
PHP mysql Er diagram
 
Cookies in php lecture 1
Cookies in php lecture 1Cookies in php lecture 1
Cookies in php lecture 1
 
Reporting using FPDF
Reporting using FPDFReporting using FPDF
Reporting using FPDF
 
Sql select
Sql select Sql select
Sql select
 
PHP mysql Mysql joins
PHP mysql  Mysql joinsPHP mysql  Mysql joins
PHP mysql Mysql joins
 
Time manipulation lecture 2
Time manipulation lecture 2Time manipulation lecture 2
Time manipulation lecture 2
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
 
Form validation server side
Form validation server side Form validation server side
Form validation server side
 
Form validation with built in functions
Form validation with built in functions Form validation with built in functions
Form validation with built in functions
 

Similar a Php Mysql (20)

Php hacku
Php hackuPhp hacku
Php hacku
 
Php 2
Php 2Php 2
Php 2
 
PHP for hacks
PHP for hacksPHP for hacks
PHP for hacks
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
 
Php basics
Php basicsPhp basics
Php basics
 
UNIT V (5).pptx
UNIT V (5).pptxUNIT V (5).pptx
UNIT V (5).pptx
 
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering CollegeDatabase Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Php verses my sql
Php verses my sqlPhp verses my sql
Php verses my sql
 
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
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Session8
Session8Session8
Session8
 
Php summary
Php summaryPhp summary
Php summary
 
veracruz
veracruzveracruz
veracruz
 
veracruz
veracruzveracruz
veracruz
 
veracruz
veracruzveracruz
veracruz
 
veracruz
veracruzveracruz
veracruz
 
Fatc
FatcFatc
Fatc
 

Más de Mudasir Syed

Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHPMudasir Syed
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction databaseMudasir Syed
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functionsMudasir Syed
 
Form validation client side
Form validation client side Form validation client side
Form validation client side Mudasir Syed
 
Javascript lecture 4
Javascript lecture  4Javascript lecture  4
Javascript lecture 4Mudasir Syed
 
Javascript lecture 3
Javascript lecture 3Javascript lecture 3
Javascript lecture 3Mudasir Syed
 
Java script lecture 1
Java script lecture 1Java script lecture 1
Java script lecture 1Mudasir Syed
 

Más de Mudasir Syed (12)

Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHP
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction database
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 
Form validation client side
Form validation client side Form validation client side
Form validation client side
 
Javascript lecture 4
Javascript lecture  4Javascript lecture  4
Javascript lecture 4
 
Javascript lecture 3
Javascript lecture 3Javascript lecture 3
Javascript lecture 3
 
Javascript 2
Javascript 2Javascript 2
Javascript 2
 
Java script lecture 1
Java script lecture 1Java script lecture 1
Java script lecture 1
 
Dom in javascript
Dom in javascriptDom in javascript
Dom in javascript
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
PHP array 2
PHP array 2PHP array 2
PHP array 2
 
PHP array 1
PHP array 1PHP array 1
PHP array 1
 

Último

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 

Último (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 

Php Mysql

  • 1.
  • 2. PHP and DatabasePHP and Database
  • 3.  MysqlMysql – popular open-source database– popular open-source database management systemmanagement system  PHPPHP usually works withusually works with MysqlMysql for web-basedfor web-based database applicationsdatabase applications  LAMPLAMP applications—Web-based applicationsapplications—Web-based applications that usethat use LynuxLynux,, ApacheApache,, MysqlMysql, and, and php/pearl/pythonphp/pearl/python
  • 4.  Connect to host server which has MysqlConnect to host server which has Mysql installedinstalled  Select a databaseSelect a database  Form an SQL statementForm an SQL statement  Execute the SQL statement and (optionally)Execute the SQL statement and (optionally) return a record setreturn a record set  Extract data from recordset using phpExtract data from recordset using php  Close connectionClose connection
  • 5. <?php<?php $host = ‘localhost’;$host = ‘localhost’; $username = ‘peter’;$username = ‘peter’; $pswd = ‘!?+&*’;$pswd = ‘!?+&*’; $dbName = “myDB”;$dbName = “myDB”; $con = mysql_connect($host, $username,$con = mysql_connect($host, $username, $pswd);$pswd); if (!$con){if (!$con){ die('Could not connect: ‘die('Could not connect: ‘ . mysql_error());. mysql_error()); }} $db = mysql_select_db($dbName,$db = mysql_select_db($dbName, $con) or die(mysql_error());$con) or die(mysql_error()); ?>?>
  • 6.  SQL  CREATE DATABASE database_name  PHP $con = mysql_connect("localhost","peter", "abc123"); $sql = “CREATE DATABASE myDB”; mysql_query(“$sql”, $con));
  • 7.  SQL  CREATE TABLE table_name (column_name1 data_type, column_name2 data_type, column_name3 data_type, .... );
  • 8.  PHP // Connect to Mysql $con = mysql_connect(. . .); // Create database mysql_query("CREATE DATABASE my_db",$con); // Select DB mysql_select_db("my_db", $con); // Create table $sql = "CREATE TABLE Persons( FirstName varchar(15), LastName varchar(15), Age int )”; // Execute SQL statement mysql_query($sql, $con); "; Contd:Contd:
  • 9.  When DB already exists:When DB already exists:  PHPPHP $con = mysql_connect("localhost","peter",$con = mysql_connect("localhost","peter", "abc123");"abc123"); $db = mysql_select_db("my_db“,$db = mysql_select_db("my_db“, $con);$con);
  • 10.  SQL SELECT colName1, colName2, colName3SELECT colName1, colName2, colName3 FROM Persons;FROM Persons;  PHP $con = mysql_connect(. . .);$con = mysql_connect(. . .); mysql_select_db("my_db“, $con);mysql_select_db("my_db“, $con); $sql = “SELECT FirstName, LastName$sql = “SELECT FirstName, LastName FROM Persons;”;FROM Persons;”; $result = mysql_query($sql);$result = mysql_query($sql);
  • 11.  PHP $result = mysql_query($sql);$result = mysql_query($sql); while($row =while($row = mysql_fetch_array($result)){mysql_fetch_array($result)){ echo $row['FirstName'] . " " .echo $row['FirstName'] . " " . $row['LastName'];$row['LastName']; echo "<br />";echo "<br />"; }}
  • 12.  SQL INSERT INTO table_nameINSERT INTO table_name VALUES (value1, value2, value3,...)VALUES (value1, value2, value3,...) oror INSERT INTO table_nameINSERT INTO table_name (column1, column2,(column1, column2, column3,...)column3,...) VALUES (value1, value2, value3,...)VALUES (value1, value2, value3,...)
  • 13. <?php $con = mysql_connect(“…",“…",“…"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("my_db", $con); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter','Griffin','35')"); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', ‘Brown', '33')"); mysql_close($con); ?> Contd:Contd:
  • 14. <html><html> <body><body> <form action="insert.php" method="post"><form action="insert.php" method="post"> Firstname: <input type="text" name=“nameF" />Firstname: <input type="text" name=“nameF" /> Lastname: <input type="text" name=“nameL" />Lastname: <input type="text" name=“nameL" /> Age: <input type="text" name="age" />Age: <input type="text" name="age" /> <input type="submit" /><input type="submit" /> </form></form> </body></body> </html></html>
  • 15. <?php $con = mysql_connect(“…",“…",“…"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("my_db", $con); $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[age]')"; if (!mysql_query($sql,$con)) die('Error: ' . mysql_error()); echo "1 record added"; mysql_close($con) ?> Contd:Contd:
  • 16.  SQL UPDATE table_nameUPDATE table_name SET column1=value, column2=value2,...SET column1=value, column2=value2,... WHERE some_column = some_value;WHERE some_column = some_value;
  • 17. <?php $con = mysql_connect(“…",“…",“…"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("my_db", $con); $sql = “UPDATE Persons SET Age = ’36’ WHERE FirstName = ‘Peter’ AND LastName = “Griffin’”; mysql_query($sql, $con); mysql_close($con); ?> Contd:Contd:
  • 18.  SQL Delete table_nameDelete table_name WHERE some_column = some_value;WHERE some_column = some_value;
  • 19. <?php $con = mysql_connect(“…",“…",“…"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("my_db", $con); $sql = “DELETE FROM Persons WHERE FirstName = ‘Peter’ AND LastName = “Griffin’”); mysql_query($sql, $con); mysql_close($con); ?> Contd:Contd: