0% found this document useful (0 votes)
49 views11 pages

PHP Mysql

The document provides information on various SQL commands used to manipulate data in MySQL tables, including INSERT, SELECT, UPDATE, DELETE, and ORDER BY. It describes the basic syntax for each command and how to use optional clauses like WHERE, LIKE, and ORDER BY to filter or sort the results.

Uploaded by

Vipul0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views11 pages

PHP Mysql

The document provides information on various SQL commands used to manipulate data in MySQL tables, including INSERT, SELECT, UPDATE, DELETE, and ORDER BY. It describes the basic syntax for each command and how to use optional clauses like WHERE, LIKE, and ORDER BY to filter or sort the results.

Uploaded by

Vipul0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Insert :

To insert data into MySQL table, you would need to use SQL INSERT
INTOcommand. You can insert data into MySQL table by using mysql>
prompt or by using any script like PHP.

Syntax:
Here is generic SQL syntax
MySQL table:

INSERT INTO command to insert data into

INSERT INTO table_name ( field1, field2,...fieldN )


VALUES
( value1, value2,...valueN );

To insert string data types, it is required to keep all the values into double
or single quote, for example:- "value".

Select
The SQL SELECT command is used to fetch data from MySQL database. You
can use this command at mysql> prompt as well as in any script like PHP.

SYNTAX:
Here is generic SQL syntax
table:

SELECT command to fetch data from MySQL

SELECT field1, field2,...fieldN table_name1, table_name2...


[WHERE Clause]
[FSET M ][LIMIT N]

You can use one or more tables separated by comma to include various
conditions using a WHERE clause, but WHERE clause is an optional part SELECT
command.

You can fetch one or more fields in a single SELECT command.

You can specify star (*) in place fields. In this case, SELECT will return all the
fields.

You can specify any condition using WHERE clause.

You can specify an fset using FSET from where SELECT will start returning
records. By default fset is zero.

You can limit the number returns using LIMIT attribute.

Where clause:
We have seen SQL SELECT command to fetch data from MySQL table. We
can use a conditional clause called WHERE clause to filter out results. Using

WHERE clause, we can specify a selection criteria to select required records


from a table.

Syntax:
Here is generic SQL syntax SELECT command with WHERE clause to fetch
data from MySQL table:
SELECT field1, field2,...fieldN table_name1, table_name2...
[WHERE condition1 [AND [OR]] condition2.....

You can use one or more tables separated by comma to include various
conditions using a WHERE clause, but WHERE clause is an optional part SELECT
command.

You can specify any condition using WHERE clause.

You can specify more than one conditions using AND or OR operators.

A WHERE clause can be used along with DELETE or UPDATE SQL command also
to specify a condition.

The WHERE clause works like an if condition in any programming language.


This clause is used to compare given value with the field value available in
MySQL table. If given value from outside is equal to the available field value
in MySQL table, then it returns that row.
Here is the list operators, which can be used with WHERE clause.
Assume field A holds 10 and field B holds 20, then:
Ope
rato
r

Descri
ption

Exa
mpl
e

Checks
if the
values
two
operan
ds are

(A =
B) is
not
true.

equal
or not,
if yes
then
conditi
on
becom
es
true.

!=

Checks
if the
values
two
operan
ds are
equal
or not,
if
values
are not
equal
then
conditi
on
becom
es
true.

(A !
= B)
is
true.

>

Checks
if the
value
left
operan
d is
greater
than
the
value
right
operan
d, if
yes
then
conditi
on
becom

(A >
B) is
not
true.

es
true.

<

Checks
if the
value
left
operan
d is
less
than
the
value
right
operan
d, if
yes
then
conditi
on
becom
es
true.

(A <
B) is
true.

>=

Checks
if the
value
left
operan
d is
greater
than or
equal
to the
value
right
operan
d, if
yes
then
conditi
on
becom
es
true.

(A
>=
B) is
not
true.

<=

Checks
if the
value
left
operan
d is
less
than or
equal
to the
value
right
operan
d, if
yes
then
conditi
on
becom
es
true.

(A
<=
B) is
true.

The WHERE clause is very useful when you want to fetch selected rows from
a table, especially when you use MySQL Join. Joins are discussed in
another chapter.
It is a common practice to search records using Primary Key to make
search fast.
If given condition does not match any record in the table, then query would
not return any row.

Update:

There may be a requirement where existing data in a MySQL table needs to


be modified. You can do so by using SQL UPDATE command. This will
modify any field value any MySQL table.

SYNTAX:
Here is generic SQL syntax UPDATE command to modify data into MySQL
table:
UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]

You can update one or more field altogether.

You can specify any condition using WHERE clause.

You can update values in a single table at a time.

The WHERE clause is very useful when you want to update selected rows in
a table.

Delete:
If you want to delete a record from any MySQL table, then you can use SQL
command DELETE FROM. You can use this command at mysql> prompt as
well as in any script like PHP.

SYNTAX:
Here is generic SQL syntax DELETE command to delete data from a MySQL
table:
DELETE FROM table_name [WHERE Clause]

If WHERE clause is not specified, then all the records will be deleted from the
given MySQL table.

You can specify any condition using WHERE clause.

You can delete records in a single table at a time.

The WHERE clause is very useful when you want to delete selected rows in
a table.

Like clause:
We have seen SQL SELECT command to fetch data from MySQL table. We
can also use a conditional clause called WHERE clause to select required
records.
A WHERE clause with equals sign (=) works fine where we want to do an
exact match. Like if "tutorial_author = 'Sanjay'". But there may be a
requirement where we want to filter out all the results where
tutorial_author name should contain "jay". This can be handled using
SQL LIKE clause along with WHERE clause.
If SQL LIKE clause is used along with % characters, then it will work like a
meta character (*) in UNIX while listing out all the files or directories at
command prompt.
Without a % character, LIKE clause is very similar to equals sign along with
WHERE clause.

SYNTAX:
Here is generic SQL syntax SELECT command along with LIKE clause to
fetch data from MySQL table:
SELECT field1, field2,...fieldN table_name1, table_name2...
WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'

You can specify any condition using WHERE clause.

You can use LIKE clause along with WHERE clause.

You can use LIKE clause in place equals sign.

When LIKE is used along with % sign then it will work like a meta character
search.

You can specify more than one conditions using AND or OR operators.

A WHERE...LIKE clause can be used along with DELETE or UPDATE SQL


command also to specify a condition.

Order by clause: (sorting)


We have seen SQL SELECT command to fetch data from MySQL table.
When you select rows, the MySQL server is free to return them in any order,
unless you instruct it otherwise by saying how to sort the result. But you
sort a result set by adding an ORDER BY clause that names the column or
columns you want to sort by.

SYNTAX:
Here is generic SQL syntax SELECT command along with ORDER BY clause
to sort data from MySQL table:
SELECT field1, field2,...fieldN table_name1, table_name2...
ORDER BY field1, [field2...] [ASC [DESC]]

You can sort returned result on any field provided that filed is being listed out.

You can sort result on more than one field.

You can use keyword ASC or DESC to get result in ascending or descending
order. By default, it's ascending order.

You can use WHERE...LIKE clause in usual way to put condition.

You might also like