PHP Mysql
PHP Mysql
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:
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:
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 star (*) in place fields. In this case, SELECT will return all the
fields.
You can specify an fset using FSET from where SELECT will start returning
records. By default fset is zero.
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
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 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.
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:
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]
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.
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'
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.
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 use keyword ASC or DESC to get result in ascending or descending
order. By default, it's ascending order.