24 Aug 2015

PHP MySQL SELECT

Display data using mysql_fetch_array( ) function

mysql_fetch_array() return the rows from the number of records available in the database as an associative array or numeric array.
at a time it return only the first row as an associative array or numeric array. if we want to retrieve all the records of the table then we must put this function inside the while loop.
Syntax

Display the records whose email is devesh@gmail.com using mysql_fetch_array()

In the above example first connection to the database is created after that the result provided by mysql_query() is passed to mysql_fetch_array() and the data of the associative array or numeric array is displayed .

Display all records from empInfo table usingmysql_fetch_array( ) function.

In the above example first connection to the database is created after that the result provided by mysql_query() is passed to mysql_fetch_array() and array returned is stored in ‘r’ variable and it put in while loop to fetch all records . after that the data is displayed in tabular form.

Mysql_fetch_assoc

Fetch data using use of mysql_fetch_assoc( ) function

mysql_fetch_assoc() return the rows from the number of records available in the database as an associative array.
at a time it return only the first row as an associative array. if we want to retrieve all the records of the table then we must put this function inside the while loop.
Syntax

Display the records whose email is devesh@gmail.com using mysql_fetch_assoc( )

In the above example first connection to the database is created after that the result provided by mysql_query() is passed to mysql_fetch_assoc() and the data of the associative array is displayed .

Display all records from empInfo table usingmysql_fetch_assoc( ) function.


Mysql_fetch_row

Fetch data using mysql_fetch_row( ) function

The mysql_fetch_row() function returns a row from a recordset as a numeric array.
mysql_fetch_row() return a single row from the number of records available in the database.
at a time it return only the first row of the result set. if we want to retrieve all the rows of the table then we must put this function inside the while loop.
Syntax

Display the records whose email is devesh@gmail.com using mysql_fetch_row( )

In the above example first connection to the database is created after that the result provided by mysql_query() is passed to mysql_fetch_row() and the data of the row is displayed using print_r() function.

Retrieve all records from empInfo table usingmysql_fetch_row( ) function.

In the above example first connection to the database is created after that the result provided by mysql_query() is passed to mysql_fetch_row() and row returned is stored in ‘r’ variable and it put in while loop to fetch all records . after that the data is displayed in tabular form.

Mysql_fetch_object

Fetch data using mysql_fetch_object( ) function

The mysql_fetch_object() function returns a row from a recordset as an object.
mysql_fetch_object() return the rows from the number of records available in the database as an object.
at a time it return only the first row as an object. if we want to retrieve all the records of the table
then we must put this function inside the while loop.
Syntax

Display the records whose email is devesh@gmail.com using mysql_fetch_object()

In the above example first connection to the database is created after that the result provided by mysql_query() is passed to mysql_fetch_object() and object returned is stored in row variable. the data of the object is displayed .

Retrieve all records from empInfo table usingmysql_fetch_object( ) function.

Output :
Emp_idNameEmailMobile
1deveshdevesh@gmail.com9910099100
2deepakdeepak@gmail.com9210053520
3raviravi@gmail.com9810098100
In the above example first connection to the database is created after that the result provided by mysql_query() is passed to mysql_fetch_object() and object returned is stored in row variable and it put in while loop to fetch all records . after that the data is displayed in tabular form.

MySQL Where Clause

Use Of MySQL Where Clause

The WHERE clause is used to extract only those records that fulfill a specified condition.
where clause is a part of select command . it is used to fetch data from a table . we basically use
this clause to filter out our results. by using where clause we can select particular set of records based
specified condition.
Syntax

The following example selects all data from the “empInfo” table where email=’devesh@gmail.com’

Output :
Emp_idNameEmailMobile
1deveshdevesh@gmail.com9910099100
In above example first of all we make connection with the database after that select query is used to search various records and a where clause is given according to which , wherever emp_id matches with devesh@gmail.com all the fields of that particular record are shown in tabular form.

MySQL Order By

How to use MySQL Order By

The ORDER BY is a keyword which is used to sort the results based on one or more columns.
by default it sorts the result set in ascending order . if we want to sort the results in
descending order we use DESC keyword. for ascending order we use ASC keyword.
Syntax

Retrieve all records from empInfo table in ascending order.

Output :
Emp_idNameEmailMobile
1deveshdevesh@gmail.com9910099100
2deepakdeepak@gmail.com9210053520
3raviravi@gmail.com9810098100
In the above example there is a empInfo table the data is retrived in ascending order based on emp_id
the fetched data is stored in the val variable and results are displayed in tabular form.

Retrieve all records from empInfo table in descending order.

Output :
Emp_idNameEmailMobile
3raviravi@gmail.com9810098100
2deepakdeepak@gmail.com9210053520
1deveshdevesh@gmail.com9910099100
In the above example first we create connection with the database and data is retrived in descending order by using DESC keyword the fetched data is stored in the val variable and results are displayed in tabular form.

Select Insert Update Delete In Php MySQL Example



1 comment:

  1. Great Blog...thanks for sharing
    I have also found the Best Web technology website to learn CRUD operation in node.js express

    ReplyDelete