May 16, 2021 MariaDB
In this chapter, we'll learn how to select data from a table.
The SELECT statement retrieves the selected row. T hey can include UNION statements, sort clauses, LIMIT clauses, WHERE clauses, GROUP BY ... H AVING clauses and sub-queries.
See the following general syntax -
SELECT field, field2,... FROM table_name, table_name2,... WHERE...
Select statements provide several options to specify which tables to use -
database_name.table_name
table_name.column_name
database_name.table_name.column_name
All select statements must contain one or more select expressions. T
he selection expression consists of one of the following options -
The column name.
Expressions that use operators and functions.
The specification "table_name ."" to select all the columns in a given table.
The character """
You can use command prompts or PHP scripts when executing select statements.
Under the command prompt, execute the following statement:
root@host# mysql -u root -p password; Enter password:******* mysql> use PRODUCTS; Database changed mysql> SELECT * from products_tbl +-------------+---------------+ | ID_number | Nomenclature | +-------------+---------------+ | 12345 | Orbitron 4000 | +-------------+---------------+
Use the same SELECT statement in the PHP function to perform the operation. Y ou will use the mysql_query() function again. S ee the example given below -
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT product_id, product_name,product_manufacturer, ship_date FROM products_tbl'; mysql_select_db('PRODUCTS'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo "Product ID :{$row['product_id']} <br> ". "Name: {$row['product_name']} <br> ". "Manufacturer: {$row['product_manufacturer']} <br> ". "Ship Date : {$row['ship_date']} <br>". "--------------------------------<br>"; } echo "Fetched data successfully "; mysql_close($conn); ?>
In a successful data retrieval, you will see the following output -
Product ID: 12345 Nomenclature: Orbitron 4000 Manufacturer: XYZ Corp Ship Date: 01/01/17 ---------------------------------------------- Product ID: 12346 Nomenclature: Orbitron 3000 Manufacturer: XYZ Corp Ship Date: 01/02/17 ---------------------------------------------- mysql> Fetched data successfully
Best practices recommend freeing cursor memory after each SELECT statement. P HP provides a mysql_free_result() function for this purpose. R eview its use as follows -
<?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT product_id, product_name, product_manufacturer, ship_date FROM products_tbl'; mysql_select_db('PRODUCTS'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_NUM)) { echo "Product ID :{$row[0]} <br> ". "Name: {$row[1]} <br> ". "Manufacturer: {$row[2]} <br> ". "Ship Date : {$row[3]} <br> ". "--------------------------------<br>"; } mysql_free_result($retval); echo "Fetched data successfully "; mysql_close($conn); ?>