MariaDB select query

In this chapter, we will learn how to select data from the table.

SELECT statement retrieves the selected row. They can include UNION statements, ordering clause, LIMIT clause, WHERE clause, GROUP BY ... HAVING clauses and sub-queries.

Review the following general syntax -

SELECT field, field2,... FROM table_name, table_name2,... WHERE...

SELECT statement provides many options for specifying table use -

  • database_name.table_name

  • table_name.column_name

  • database_name.table_name.column_name

All the select statement must include one or more select expressions. Select one of the following options composed by the expression -

  • Column name.

  • Using expression operators and functions.

  • Norms "table_name. *" To select all columns for a given table.

  • Character "*" to select all columns from all the tables in the FROM clause specifies.

You can use the command prompt or PHP script for select statements.

Command Prompt

At 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 |
+-------------+---------------+

Select PHP script

To perform the same operation using the SELECT statement in PHP function. You will once again use the mysql_query () function. See the examples 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 practice recommendations released cursor memory after each SELECT statement. PHP aims to provide a mysql_free_result () function. Review its use is 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);
?>

This switched: http: //codingdict.com/article/7093

Guess you like

Origin www.cnblogs.com/bczd/p/12009679.html