SlideShare a Scribd company logo
MySQL COMMANDS AND QUERYING
Prasanna Pabba
15071D2510
1
https://www.youtube.com/watch?v=mpQts3ezPVg
What and Why Iam Teaching:
Mysql commands and Quering In
PHP-MYSQL
To build carrer in the web development
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 2
PHP MySQL Introduction
What is Mysql?
MySql is a Database Server and supports Standard Sql.
Mysql is ideal for both Small and Large applications.
It complies on no of platforms and free to download and use.
What is Php Mysql?
Php combined with Mysql are cross platform(you can develop
in windows and serve on a Unix platform)
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 3
CREATE
create databases and tables
SELECT
select table rows based on certain conditions
DELETE
delete one or more rows of a table
INSERT
Insert a new row in a table
UPDATE
update rows in a table
Where
This clause is used to extract on those records that fullfill a specified Criterion
OrderBy
OrderBy Keyword is used to sort the data in a record set
Basic Commands On PHP MySql
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 4
Quering Mysql
Queries
A query is a question or a request.
With MySQL, we can query a database for
specific information and have a recordset
returned.
Look at the following query:
SELECT LastName FROM Persons
The query above selects all the data in the
"LastName" column from the "Persons"
table, and will return a recordset like this:
LastName
Griffin
Quagmire
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 5
PHP MySql Connect To a DataBase
Create a Connection to a MySQL Database
Before you can access data in a database, you must create a connection to the
database.
In PHP, this is done with the mysql_connect() function.
Syntax
mysql_connect(servername,username,password);
Parameter Description
Servername Optional. Specifies the server to connect to. Default value is
"localhost:3306“
Username Optional. Specifies the username to log in with. Default value
is the name of the user that owns the server process
passwordOptional. Specifies the password to log in with. Default is “"
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 6
Sample Example For Mysql Connect
Example
In the following example we store the connection in a variable
($con) for later use in the script. The "die" part will be executed
if the connection fails:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 7
PHP MySQL Create Database and Tables
CREATE DATABASE database_name
To get PHP to execute the statement above we must use the mysql_query() function.
This function is used to send a query or command to a MySQL connection.
Example
The following example creates a database called "my_db":
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?>
Output:
Database Created
Prasanna Pabba 15071D2505 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 8
PHP Mysql Create a Table
The CREATE TABLE statement is used to create a table in
MySQL.
Syntax
CREATE TABLE table_name
(column_name1 data_type,
column_name2 data_type,
column_name3 data_type,....)
We must add the CREATE TABLE statement to the
mysql_query() function to execute the command
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 9
Example Of a Create Table
The following example creates a table named "Persons", with three columns. The column names will be "FirstName", "LastName" and "Age":
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else Output
{ FirstName LastName Age
echo "Error creating database: " . mysql_error();
}
// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
// Execute query
mysql_query($sql,$con);
mysql_close($con);
?>
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 10
PHP MySQL Insert Into
Insert Data Into a Database Table
The INSERT INTO statement is used to add new records to a database
table.
Syntax
It is possible to write the INSERT INTO statement in two forms.
The first form doesn't specify the column names where the data will
be inserted, only their values:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
The second form specifies both the column names and the values to
be inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
To get PHP to execute the statements above we must use the
mysql_query() function. This function is used to send a query or
command to a MySQL connection
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 11
Example of a insert into
<?php
$con = mysql_connect("localhost","peter","abc123");
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', 'Quagmire', '33')");
mysql_close($con);
?>
Output: Persons
FirstName LastName Age
Peter Griffin 35
Glenn Quagmire 33
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 12
PHP MySQL Select
Select Data From a Database Table
The SELECT statement is used to select data
from a database.
Syntax
SELECT column_name(s)
FROM table_name
To get PHP to execute the statement above
we must use the mysql_query() function. This
function is used to send a query or command
to a MySQL connection.
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 13
Example Of Php Mysql select
Example
The following example selects all the data stored in the "Persons" table (The * character selects all the
data in the table):
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
mysql_close($con);
?>
Output:
Peter Griffin
Glenn Quagmire
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 14
Display the Result in an HTML Table
The following example selects the same data as the example above, but will display the data in an HTML
table:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 15
The output of the code above will be:
FirstName
LastName
Glenn
QuAgmire
Peter Griffin
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 16
PHP MySQL The Where Clause
The WHERE clause
The WHERE clause is used to extract only those records that fulfill a
specified criterion.
Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
To get PHP to execute the statement above we must use the
mysql_query() function.
This function is used to send a query or command to a MySQL
connection.
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 17
Example Of Php Mysql Where Class
example
The following example selects all rows from the "Persons" table where "FirstName='Peter':
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons
WHERE FirstName='Peter'");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
?>
The output of the code
above will be:
Peter Griffin
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 18
PHP MySQL Order By Keyword
The ORDER BY keyword is used to sort the data in a recordset.
The ORDER BY keyword sort the records in ascending order by
default.
If you want to sort the records in a descending order, you can use
the DESC keyword.
Syntax
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 19
Example By OrderBy Clause
Example
The following example selects all the data stored in the "Persons" table, and sorts the result by the "Age" column:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons ORDER BY age");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'];
echo " " . $row['LastName'];
echo " " . $row['Age'];
echo "<br />";
}
mysql_close($con);
?>
The output of the
code above will be:
Glenn Quagmire 33
Peter Griffin 35
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 20
PHP MySQL Update
The UPDATE statement is used to modify data in a table.
Update Data In a Database
The UPDATE statement is used to update existing records in a table.
Syntax
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE
clause specifies which record or records that should be updated. If
you omit the WHERE clause, all records will be updated!
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 21
Example of Update Clause
The following example updates some data in the "Persons" table:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("UPDATE Persons SET Age = '36'
WHERE FirstName = 'Peter' AND
LastName = 'Griffin'");
mysql_close($con);
?>
After the update, the "Persons" table
will look like this:
FirstName LastName Age
Peter Griffin 36
Glenn Quagmire 33
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 22
PHP MySQL Delete
The DELETE statement is used to delete records in a table.
Delete Data In a Database
The DELETE FROM statement is used to delete records from a
database table.
Syntax
DELETE FROM table_name
WHERE some_column = some_value
Note: Notice the WHERE clause in the DELETE syntax. The WHERE
clause specifies which record or records that should be deleted. If
you omit the WHERE clause, all records will be deleted!
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 23
Example Of Deletion
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");
mysql_close($con);
?>
After the deletion, the table will look like this:
FirstName LastName Age
Glenn Quagmire 33
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 24
Summary
CREATE
create databases and tables
mysql_query("CREATE DATABASE my_db",$con)
SELECT
select table rows based on certain conditions
INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)
DELETE
delete one or more rows of a table
DELETE FROM table_name WHERE some_column = some_value
INSERT
Insert a new row in a table
UPDATE
update rows in a table
UPDATE table_name SET column1=value, column2=value2,...WHEREsome_column=some_value
Where
This clause is used to extract on those records that fullfill a specified Criterion
OrderBy
OrderBy Keyword is used to sort the data in a record set
SELECT column_name(s)FROM table_name ORDER BY column_name(s) ASC|DESC
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 25
References
• Beginning Php And Mysql: From Novice To
Professional, 4Th Ed
Head First PHP & MySQL (A Brain-Friendly
Guide) by Lynn Beighley (Author), Michael Morrison
• http://www.w3schools.com/php/php_mysql_intro.asp
• http://www.tutorialspoint.com/php_mysql_online.php
Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 26
Ad

More Related Content

What's hot (18)

Database Connection With Mysql
Database Connection With MysqlDatabase Connection With Mysql
Database Connection With Mysql
Harit Kothari
 
Nagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and Nagios
Nagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and NagiosNagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and Nagios
Nagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and Nagios
Nagios
 
When dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniquesWhen dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniques
Wim Godden
 
Quebec pdo
Quebec pdoQuebec pdo
Quebec pdo
Valentine Dianov
 
Php101
Php101Php101
Php101
Ömer Taşkın
 
Php 101: PDO
Php 101: PDOPhp 101: PDO
Php 101: PDO
Jeremy Kendall
 
Beginner guide to mysql command line
Beginner guide to mysql command lineBeginner guide to mysql command line
Beginner guide to mysql command line
Priti Solanki
 
MySQL
MySQLMySQL
MySQL
Gouthaman V
 
Agile database access with CakePHP 3
Agile database access with CakePHP 3Agile database access with CakePHP 3
Agile database access with CakePHP 3
José Lorenzo Rodríguez Urdaneta
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
Pierre MARTIN
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHP
markstory
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
Wim Godden
 
lab56_db
lab56_dblab56_db
lab56_db
tutorialsruby
 
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injectionStHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
StHack
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
Wez Furlong
 
Nginx and friends - putting a turbo button on your site
Nginx and friends - putting a turbo button on your siteNginx and friends - putting a turbo button on your site
Nginx and friends - putting a turbo button on your site
Wim Godden
 
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp KrennJavantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
MongoDB Replication (Dwight Merriman)
MongoDB Replication (Dwight Merriman)MongoDB Replication (Dwight Merriman)
MongoDB Replication (Dwight Merriman)
MongoSF
 
Database Connection With Mysql
Database Connection With MysqlDatabase Connection With Mysql
Database Connection With Mysql
Harit Kothari
 
Nagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and Nagios
Nagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and NagiosNagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and Nagios
Nagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and Nagios
Nagios
 
When dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniquesWhen dynamic becomes static: the next step in web caching techniques
When dynamic becomes static: the next step in web caching techniques
Wim Godden
 
Beginner guide to mysql command line
Beginner guide to mysql command lineBeginner guide to mysql command line
Beginner guide to mysql command line
Priti Solanki
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
Pierre MARTIN
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHP
markstory
 
Caching and tuning fun for high scalability
Caching and tuning fun for high scalabilityCaching and tuning fun for high scalability
Caching and tuning fun for high scalability
Wim Godden
 
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injectionStHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
StHack
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
Wez Furlong
 
Nginx and friends - putting a turbo button on your site
Nginx and friends - putting a turbo button on your siteNginx and friends - putting a turbo button on your site
Nginx and friends - putting a turbo button on your site
Wim Godden
 
MongoDB Replication (Dwight Merriman)
MongoDB Replication (Dwight Merriman)MongoDB Replication (Dwight Merriman)
MongoDB Replication (Dwight Merriman)
MongoSF
 

Viewers also liked (10)

PHP - Introduction to PHP MySQL Joins and SQL Functions
PHP -  Introduction to PHP MySQL Joins and SQL FunctionsPHP -  Introduction to PHP MySQL Joins and SQL Functions
PHP - Introduction to PHP MySQL Joins and SQL Functions
Vibrant Technologies & Computers
 
PHP BASIC PRESENTATION
PHP BASIC PRESENTATIONPHP BASIC PRESENTATION
PHP BASIC PRESENTATION
krutitrivedi
 
Php My Sql Security 2007
Php My Sql Security 2007Php My Sql Security 2007
Php My Sql Security 2007
Aung Khant
 
Mysql Ppt
Mysql PptMysql Ppt
Mysql Ppt
Hema Prasanth
 
student supervision system
student supervision systemstudent supervision system
student supervision system
Dhruti Ranjan Bag
 
Php basic
Php basicPhp basic
Php basic
argusacademy
 
Linux
LinuxLinux
Linux
Kavi Bharathi R
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
webhostingguy
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
Manish Bothra
 
Ad

Similar to Php mysq (20)

chapter_Seven Database manipulation using php.pptx
chapter_Seven Database manipulation using php.pptxchapter_Seven Database manipulation using php.pptx
chapter_Seven Database manipulation using php.pptx
Getawu
 
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
Firdaus Adib
 
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
berihun18
 
Mysql & Php
Mysql & PhpMysql & Php
Mysql & Php
Inbal Geffen
 
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
Arti Parab Academics
 
UNIT V (5).pptx
UNIT V (5).pptxUNIT V (5).pptx
UNIT V (5).pptx
DrDhivyaaCRAssistant
 
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
Dhivyaa C.R
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
veracruz
veracruzveracruz
veracruz
tutorialsruby
 
veracruz
veracruzveracruz
veracruz
tutorialsruby
 
veracruz
veracruzveracruz
veracruz
tutorialsruby
 
Lecture6 display data by okello erick
Lecture6 display data by okello erickLecture6 display data by okello erick
Lecture6 display data by okello erick
okelloerick
 
PHP with MySQL
PHP with MySQLPHP with MySQL
PHP with MySQL
wahidullah mudaser
 
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
saritasingh19866
 
All Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for NewbiesAll Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for Newbies
Dave Stokes
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
Jalpesh Vasa
 
Python database access
Python database accessPython database access
Python database access
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docxModule 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
moirarandell
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
Database presentation
Database presentationDatabase presentation
Database presentation
webhostingguy
 
chapter_Seven Database manipulation using php.pptx
chapter_Seven Database manipulation using php.pptxchapter_Seven Database manipulation using php.pptx
chapter_Seven Database manipulation using php.pptx
Getawu
 
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
Firdaus Adib
 
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
berihun18
 
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
Arti Parab Academics
 
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
Dhivyaa C.R
 
Lecture6 display data by okello erick
Lecture6 display data by okello erickLecture6 display data by okello erick
Lecture6 display data by okello erick
okelloerick
 
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
saritasingh19866
 
All Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for NewbiesAll Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for Newbies
Dave Stokes
 
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docxModule 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
Module 6WEB SERVER AND SERVER SIDE SCRPTING, PART-2Chapte.docx
moirarandell
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
Database presentation
Database presentationDatabase presentation
Database presentation
webhostingguy
 
Ad

Recently uploaded (20)

Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In FranceManifest Pre-Seed Update | A Humanoid OEM Deeptech In France
Manifest Pre-Seed Update | A Humanoid OEM Deeptech In France
chb3
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 

Php mysq

  • 1. MySQL COMMANDS AND QUERYING Prasanna Pabba 15071D2510 1 https://www.youtube.com/watch?v=mpQts3ezPVg What and Why Iam Teaching: Mysql commands and Quering In PHP-MYSQL To build carrer in the web development
  • 2. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 2 PHP MySQL Introduction What is Mysql? MySql is a Database Server and supports Standard Sql. Mysql is ideal for both Small and Large applications. It complies on no of platforms and free to download and use. What is Php Mysql? Php combined with Mysql are cross platform(you can develop in windows and serve on a Unix platform)
  • 3. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 3 CREATE create databases and tables SELECT select table rows based on certain conditions DELETE delete one or more rows of a table INSERT Insert a new row in a table UPDATE update rows in a table Where This clause is used to extract on those records that fullfill a specified Criterion OrderBy OrderBy Keyword is used to sort the data in a record set Basic Commands On PHP MySql
  • 4. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 4 Quering Mysql Queries A query is a question or a request. With MySQL, we can query a database for specific information and have a recordset returned. Look at the following query: SELECT LastName FROM Persons The query above selects all the data in the "LastName" column from the "Persons" table, and will return a recordset like this: LastName Griffin Quagmire
  • 5. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 5 PHP MySql Connect To a DataBase Create a Connection to a MySQL Database Before you can access data in a database, you must create a connection to the database. In PHP, this is done with the mysql_connect() function. Syntax mysql_connect(servername,username,password); Parameter Description Servername Optional. Specifies the server to connect to. Default value is "localhost:3306“ Username Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process passwordOptional. Specifies the password to log in with. Default is “"
  • 6. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 6 Sample Example For Mysql Connect Example In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code ?>
  • 7. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 7 PHP MySQL Create Database and Tables CREATE DATABASE database_name To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection. Example The following example creates a database called "my_db": <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } mysql_close($con); ?> Output: Database Created
  • 8. Prasanna Pabba 15071D2505 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 8 PHP Mysql Create a Table The CREATE TABLE statement is used to create a table in MySQL. Syntax CREATE TABLE table_name (column_name1 data_type, column_name2 data_type, column_name3 data_type,....) We must add the CREATE TABLE statement to the mysql_query() function to execute the command
  • 9. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 9 Example Of a Create Table The following example creates a table named "Persons", with three columns. The column names will be "FirstName", "LastName" and "Age": <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // Create database if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else Output { FirstName LastName Age echo "Error creating database: " . mysql_error(); } // Create table mysql_select_db("my_db", $con); $sql = "CREATE TABLE Persons ( FirstName varchar(15), LastName varchar(15), Age int )"; // Execute query mysql_query($sql,$con); mysql_close($con); ?>
  • 10. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 10 PHP MySQL Insert Into Insert Data Into a Database Table The INSERT INTO statement is used to add new records to a database table. Syntax It is possible to write the INSERT INTO statement in two forms. The first form doesn't specify the column names where the data will be inserted, only their values: INSERT INTO table_name VALUES (value1, value2, value3,...) The second form specifies both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) To get PHP to execute the statements above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection
  • 11. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 11 Example of a insert into <?php $con = mysql_connect("localhost","peter","abc123"); 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', 'Quagmire', '33')"); mysql_close($con); ?> Output: Persons FirstName LastName Age Peter Griffin 35 Glenn Quagmire 33
  • 12. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 12 PHP MySQL Select Select Data From a Database Table The SELECT statement is used to select data from a database. Syntax SELECT column_name(s) FROM table_name To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.
  • 13. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 13 Example Of Php Mysql select Example The following example selects all the data stored in the "Persons" table (The * character selects all the data in the table): <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } mysql_close($con); ?> Output: Peter Griffin Glenn Quagmire
  • 14. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 14 Display the Result in an HTML Table The following example selects the same data as the example above, but will display the data in an HTML table: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons"); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?>
  • 15. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 15 The output of the code above will be: FirstName LastName Glenn QuAgmire Peter Griffin
  • 16. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 16 PHP MySQL The Where Clause The WHERE clause The WHERE clause is used to extract only those records that fulfill a specified criterion. Syntax SELECT column_name(s) FROM table_name WHERE column_name operator value To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection.
  • 17. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 17 Example Of Php Mysql Where Class example The following example selects all rows from the "Persons" table where "FirstName='Peter': <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } ?> The output of the code above will be: Peter Griffin
  • 18. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 18 PHP MySQL Order By Keyword The ORDER BY keyword is used to sort the data in a recordset. The ORDER BY keyword sort the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword. Syntax SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC
  • 19. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 19 Example By OrderBy Clause Example The following example selects all the data stored in the "Persons" table, and sorts the result by the "Age" column: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons ORDER BY age"); while($row = mysql_fetch_array($result)) { echo $row['FirstName']; echo " " . $row['LastName']; echo " " . $row['Age']; echo "<br />"; } mysql_close($con); ?> The output of the code above will be: Glenn Quagmire 33 Peter Griffin 35
  • 20. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 20 PHP MySQL Update The UPDATE statement is used to modify data in a table. Update Data In a Database The UPDATE statement is used to update existing records in a table. Syntax UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
  • 21. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 21 Example of Update Clause The following example updates some data in the "Persons" table: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'"); mysql_close($con); ?> After the update, the "Persons" table will look like this: FirstName LastName Age Peter Griffin 36 Glenn Quagmire 33
  • 22. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 22 PHP MySQL Delete The DELETE statement is used to delete records in a table. Delete Data In a Database The DELETE FROM statement is used to delete records from a database table. Syntax DELETE FROM table_name WHERE some_column = some_value Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!
  • 23. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 23 Example Of Deletion <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("DELETE FROM Persons WHERE LastName='Griffin'"); mysql_close($con); ?> After the deletion, the table will look like this: FirstName LastName Age Glenn Quagmire 33
  • 24. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 24 Summary CREATE create databases and tables mysql_query("CREATE DATABASE my_db",$con) SELECT select table rows based on certain conditions INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...) DELETE delete one or more rows of a table DELETE FROM table_name WHERE some_column = some_value INSERT Insert a new row in a table UPDATE update rows in a table UPDATE table_name SET column1=value, column2=value2,...WHEREsome_column=some_value Where This clause is used to extract on those records that fullfill a specified Criterion OrderBy OrderBy Keyword is used to sort the data in a record set SELECT column_name(s)FROM table_name ORDER BY column_name(s) ASC|DESC
  • 25. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 25 References • Beginning Php And Mysql: From Novice To Professional, 4Th Ed Head First PHP & MySQL (A Brain-Friendly Guide) by Lynn Beighley (Author), Michael Morrison • http://www.w3schools.com/php/php_mysql_intro.asp • http://www.tutorialspoint.com/php_mysql_online.php
  • 26. Prasanna Pabba 15071D2510 SCRIPTING LANGUAGES MTECH 1ST YEAR VNRVJIET 26