In this part, we will figure out how to choose information from a table.
SELECT articulations recover chosen lines. They can incorporate UNION explanations, a requesting condition, a LIMIT proviso, a WHERE statement, a GROUP BY...HAVING proviso, and subqueries.
Audit the accompanying general grammar −
SELECT field, field2,... FROM table_name, table_name2,... WHERE...
A SELECT assertion gives various choices to indicating the table utilized −
- database_name.table_name
- table_name.column_name
- database_name.table_name.column_name
All select assertions should contain at least one select articulations. Select articulations comprise of one of the accompanying choices −
- A section name.
- An articulation utilizing administrators and capacities.
- The detail "table_name.*" to choose all segments inside the given table.
- The character "*" to choose all sections from all tables indicated in the FROM condition.
The order immediate or a PHP content can be utilized in executing a select assertion.
The Command Prompt
At the order brief, execute explanations as follows −
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 |
+-------------+---------------+
PHP Select Script
Utilize a similar SELECT statement(s) inside a PHP capacity to play out the activity. You will utilize the mysql_query() work indeed. Audit a model given underneath −
<?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\n";
mysql_close($conn);
?>
On fruitful information recovery, you will see the accompanying yield −
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 delivering cursor memory after each SELECT articulation. PHP gives the mysql_free_result() work for this reason. Audit its utilization as demonstrated beneath −
<?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\n";
mysql_close($conn);
?>