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

More Related Content

PDF
Using php with my sql
PDF
Stored Procedure
PDF
PHP and Mysql
PDF
veracruz
ODP
My app is secure... I think
ODP
Beyond PHP - it's not (just) about the code
PPT
Php Mysql
Using php with my sql
Stored Procedure
PHP and Mysql
veracruz
My app is secure... I think
Beyond PHP - it's not (just) about the code
Php Mysql

What's hot (18)

ODP
Database Connection With Mysql
PDF
Nagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and Nagios
ODP
When dynamic becomes static: the next step in web caching techniques
PPT
Quebec pdo
PDF
KEY
Php 101: PDO
PPTX
Beginner guide to mysql command line
PPT
PDF
Agile database access with CakePHP 3
PDF
Internationalizing CakePHP Applications
PDF
Future of HTTP in CakePHP
ODP
Caching and tuning fun for high scalability
PDF
lab56_db
PDF
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
PDF
PHP Data Objects
ODP
Nginx and friends - putting a turbo button on your site
PDF
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
PPTX
MongoDB Replication (Dwight Merriman)
Database Connection With Mysql
Nagios Conference 2013 - Sheeri Cabral - Alerting With MySQL and Nagios
When dynamic becomes static: the next step in web caching techniques
Quebec pdo
Php 101: PDO
Beginner guide to mysql command line
Agile database access with CakePHP 3
Internationalizing CakePHP Applications
Future of HTTP in CakePHP
Caching and tuning fun for high scalability
lab56_db
StHack 2013 - Florian "@agixid" Gaultier No SQL injection but NoSQL injection
PHP Data Objects
Nginx and friends - putting a turbo button on your site
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
MongoDB Replication (Dwight Merriman)
Ad

Viewers also liked (10)

PPT
PHP - Introduction to PHP MySQL Joins and SQL Functions
ODP
PHP BASIC PRESENTATION
PPT
Php My Sql Security 2007
PPT
Mysql Ppt
DOCX
student supervision system
PPSX
Php basic
PPT
PPT
MySql slides (ppt)
PPT
PPT
Php Presentation
PHP - Introduction to PHP MySQL Joins and SQL Functions
PHP BASIC PRESENTATION
Php My Sql Security 2007
Mysql Ppt
student supervision system
Php basic
MySql slides (ppt)
Php Presentation
Ad

Similar to Php mysq (20)

PPTX
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
PPTX
Learn PHP Lacture2
PPT
PHP - Getting good with MySQL part II
PPTX
Mysql
PPTX
3-Chapter-Edit.pptx debre tabour university
PPT
Chapter 09 php my sql
PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT V (5).pptx
PPTX
3 php-connect-to-my sql
PPTX
chapter_Seven Database manipulation using php.pptx
PDF
PHP with MySQL
PDF
Mysql & Php
PDF
4.3 MySQL + PHP
PPT
MYSQL - PHP Database Connectivity
PPT
My sql with querys
PDF
Php workshop L04 database
PDF
Php 2
PDF
The Ring programming language version 1.9 book - Part 36 of 210
PDF
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Learn PHP Lacture2
PHP - Getting good with MySQL part II
Mysql
3-Chapter-Edit.pptx debre tabour university
Chapter 09 php my sql
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT V (5).pptx
3 php-connect-to-my sql
chapter_Seven Database manipulation using php.pptx
PHP with MySQL
Mysql & Php
4.3 MySQL + PHP
MYSQL - PHP Database Connectivity
My sql with querys
Php workshop L04 database
Php 2
The Ring programming language version 1.9 book - Part 36 of 210
MySQL Baics - Texas Linxufest beginners tutorial May 31st, 2019

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
KodekX | Application Modernization Development
PPTX
Cloud computing and distributed systems.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Big Data Technologies - Introduction.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
Teaching material agriculture food technology
Spectral efficient network and resource selection model in 5G networks
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Advanced Soft Computing BINUS July 2025.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
KodekX | Application Modernization Development
Cloud computing and distributed systems.
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Monthly Chronicles - July 2025
Big Data Technologies - Introduction.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Review of recent advances in non-invasive hemoglobin estimation
Mobile App Security Testing_ A Comprehensive Guide.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Reach Out and Touch Someone: Haptics and Empathic Computing
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Per capita expenditure prediction using model stacking based on satellite ima...

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