Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL Querying Data

MySQL Querying Data

MySQL databases use the SELECT statement to query data.

You can query data in the database through the mysql> command prompt, or through PHP scripts.

Syntax

The following is the general SELECT syntax for querying data in a MySQL database:



SELECT column1, column2, ...

FROM table_name

[WHERE condition]

[ORDER BY column_name [ASC | DESC]]

[LIMIT number];

Parameter Description:

Simple application examples of MySQL SELECT statements:

Example

-- Select all rows of all columns

SELECT

FROM

users;

-- Select all rows of specific columns

SELECT

username

,

email

FROM

users;

-- Add a WHERE clause to select rows that meet the conditions

SELECT

FROM

users

WHERE

is_active

=

TRUE

;

-- Add an ORDER BY clause to sort by a column in ascending order

SELECT

FROM

users

ORDER

BY

birthdate;

-- Add an ORDER BY clause to sort by a column in descending order

SELECT

FROM

users

ORDER

BY

birthdate

DESC

;

-- Add a LIMIT clause to limit the number of returned rows

SELECT

FROM

users

LIMIT

10

;

SELECT statements can be flexible. We can combine and use these clauses according to actual needs, such as using WHERE and ORDER BY clauses together, or using LIMIT to control the number of returned rows.

in WHERE In the clause, you can use various conditional operators (such as =, <, >, <=, >=, !=), logical operators (such as AND, OR, NOT), and wildcards (such as %), and so on.

Here are some advanced SELECT statement examples:

Example

-- Use the AND operator and wildcards

SELECT

FROM

users

WHERE

username

LIKE

'j%'

AND

is_active

=

TRUE

;

-- Use the OR operator

SELECT

FROM

users

WHERE

is_active

=

TRUE

OR

birthdate

<

'1990-01-01'

;

-- Use the IN clause

SELECT

FROM

users

WHERE

birthdate

IN

(

'1990-01-01'

,

'1992-03-15'

,

'1993-05-03'

)

;


Retrieve data through the command prompt

In the following example, we will use the SQL SELECT command to retrieve data from the MySQL table runoob_tbl:

Example

The following example will return all records from the table runoob_tbl:

Read the data table:

select

from

runoob_tbl

;

Output result:


Retrieve data using PHP scripts

Use the PHP function mysqli_query() with the SQL SELECT command to retrieve data.

This function is used to execute SQL commands, and then the PHP function mysqli_fetch_array() is used to use or output all the queried data.

The mysqli_fetch_array() function fetches a row from the result set as an associative array, a numeric array, or both. It returns an array generated from the row fetched from the result set, or false if there are no more rows.

The following example reads all records from the table runoob_tbl.

Example

Try the following example to display all records in the table runoob_tbl.

Retrieve data using the mysqli_fetch_array MYSQLI_ASSOC parameter:

Runoob Tutorial mysqli_fetch_array Test ' ; echo ' ' ; while ( $row = mysqli_fetch_array ( $retval , MYSQLI_ASSOC ) ) { echo " " . " " . " " . " " . " " ; } echo '
Tutorial ID Title Author Submission Date
{$row['runoob_id']}{$row['runoob_title']}{$row['runoob_author']}{$row['submission_date']}
' ; mysqli_close ( $conn ) ; ?>

The output is shown below:

In the above example, each row read is assigned to the variable $row, and then each value is printed.

**Note:** Remember that if you need to use a variable inside a string, place the variable in curly braces.

In the above example, the second parameter of the PHP mysqli_fetch_array() function is MYSQLI_ASSOC. Setting this parameter makes the query result return an associative array, and you can use field names as array indexes.

PHP provides another function mysqli_fetch_assoc(), which fetches a row from the result set as an associative array. It returns an associative array generated from the row fetched from the result set, or false if there are no more rows.

Example

Try the following example, which uses mysqli_fetch_assoc() function to output all records in the table runoob_tbl:

Retrieve data using mysqli_fetch_assoc:

Runoob Tutorial mysqli_fetch_assoc Test ' ; echo ' ' ; while ( $row = mysqli_fetch_assoc ( $retval ) ) { echo " " . " " . " " . " " . " " ; } echo '
Tutorial ID Title Author Submission Date
{$row['runoob_id']}{$row['runoob_title']}{$row['runoob_author']}{$row['submission_date']}
' ; mysqli_close ( $conn ) ; ?>

The output is shown below:

You can also use the constant MYSQLI_NUM as the second parameter of the PHP mysqli_fetch_array() function to return a numeric array.

Example

The following example uses the ** MYSQLI_NUM ** parameter to display all records in the table runoob_tbl:

Retrieve data using the mysqli_fetch_array MYSQLI_NUM parameter:

Runoob Tutorial mysqli_fetch_array Test ' ; echo ' ' ; while ( $row = mysqli_fetch_array ( $retval , MYSQLI_NUM ) ) { echo " " . " " . " " . " " . " " ; } echo '
Tutorial ID Title Author Submission Date
{$row[0]}{$row[1]}{$row[2]}{$row[3]}
' ; mysqli_close ( $conn ) ; ?>

The output is shown below:

All three examples above produce the same output.


Memory release

After we finish executing the SELECT statement, it is a good habit to release the cursor memory.

Memory release can be achieved through the PHP function mysqli_free_result().

The following example demonstrates how to use this function.

Example

Try the following example:

Release memory using mysqli_free_result:

Runoob Tutorial mysqli_fetch_array Test ' ; echo ' ' ; while ( $row = mysqli_fetch_array ( $retval , MYSQLI_NUM ) ) { echo " " . " " . " " . " " . " " ; } echo '
Tutorial ID Title Author Submission Date
{$row[0]}{$row[1]}{$row[2]}{$row[3]}
' ; // Release memory mysqli_free_result ( $retval ) ; mysqli_close ( $conn ) ; ?>

The output is shown below: