MySQLi Tutorial on MySQLi Select Query

the sql select command is used to fetch data from the mysql database. you can use this command at mysql> prompt as well as in any script like php.

syntax

here is generic sql syntax of select command to fetch data from the mysql table −

select field1, field2,...fieldn 
from table_name1, table_name2...
[where clause]
[offset m ][limit n]
  • you can use one or more tables separated by comma to include various conditions using a where clause, but the where clause is an optional part of the select command.

  • you can fetch one or more fields in a single select command.

  • you can specify star (*) in place of fields. in this case, select will return all the fields.

  • you can specify any condition using the where clause.

  • you can specify an offset using offset from where select will start returning records. by default, the offset starts at zero.

  • you can limit the number of returns using the limit attribute.

fetching data from a command prompt

this will use sql select command to fetch data from the mysql table tutorials_tbl.

example

the following example will return all the records from the tutorials_tbl table −

root@host# mysql -u root -p password;
enter password:*******
mysql> use tutorials;
database changed
mysql> select * from tutorials_tbl 
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|           1 | learn php      | john poul       | 2007-05-21      |
|           2 | learn mysql    | abdul s         | 2007-05-21      |
|           3 | java tutorial  | sanjay          | 2007-05-21      |
+-------------+----------------+-----------------+-----------------+
3 rows in set (0.01 sec)

mysql>

fetching data using a php script

php uses mysqli query() or mysql_query() function to select records from a mysql table. this function takes two parameters and returns true on success or false on failure.

syntax

$mysqli→query($sql,$resultmode)

sr.no. parameter & description
1

$sql

required - sql query to select records from a mysql table.

2

$resultmode

optional - either the constant mysqli_use_result or mysqli_store_result depending on the desired behavior. by default, mysqli_store_result is used.

example

try the following example to select a record from a table −

copy and paste the following example as mysql_example.php −

<html>
   <head>
      <title>creating mysql table</title>
   </head>
   <body>
      <?php
         $dbhost = 'localhost';
         $dbuser = 'root';
         $dbpass = 'root@123';
         $dbname = 'tutorials';
         $mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
         
         if($mysqli→connect_errno ) {
            printf("connect failed: %s<br />", $mysqli→connect_error);
            exit();
         }
         printf('connected successfully.<br />');
   
         $sql = "select tutorial_id, tutorial_title, tutorial_author, submission_date from tutorials_tbl";
		 
         $result = $mysqli->query($sql);
           
         if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
               printf("id: %s, title: %s, author: %s, date: %d <br />", 
                  $row["tutorial_id"], 
                  $row["tutorial_title"], 
                  $row["tutorial_author"],
                  $row["submission_date"]);               
            }
         } else {
            printf('no record found.<br />');
         }
         mysqli_free_result($result);
         $mysqli→close();
      ?>
   </body>
</html>

output

access the mysql_example.php deployed on apache web server and verify the output. here we've entered multiple records in the table before running the select script.

connected successfully.
id: 1, title: mysql tutorial, author: mahesh, date: 2021
id: 2, title: html tutorial, author: mahesh, date: 2021
id: 3, title: php tutorial, author: mahesh, date: 2021
id: 4, title: java tutorial, author: mahesh, date: 2021
id: 5, title: apache tutorial, author: suresh, date: 2021