The SQL SELECT order is utilized to get information from MySQLi data set. You can utilize this order at mysql> speedy just as in any content like PHP.
Syntax
Here is nonexclusive SQL linguistic structure of SELECT order to get information from MySQLi table −
SELECT field1, field2,...fieldN table_name1, table_name2...
[WHERE Clause]
[OFFSET M ][LIMIT N]
You can utilize at least one tables isolated by comma to incorporate different conditions utilizing a WHERE proviso, yet WHERE statement is a discretionary piece of SELECT order.
- You can get at least one fields in a solitary SELECT order.
- You can indicate star (*) instead of fields. For this situation, SELECT will restore all the fields.
- You can determine any condition utilizing WHERE statement.
- You can indicate a balance utilizing OFFSET from where SELECT will begin bringing records back. Of course balance is zero.
- You can restrict the quantity of profits utilizing LIMIT quality.
Fetching Data from Command Prompt −
This will utilize SQL SELECT order to get information from MySQLi table tutorials_tbl
Example
Following model will restore all the records from tutorials_inf table −
root@host# mysql -u root -p password;
Enter password:*******
mysql> use TUTORIALS;
Database changed
mysql> SELECT * from tutorials_inf;
+----+------+
| id | name |
+----+------+
| 1 | sai |
| 2 | kit |
| 3 | ram |
+----+------+
3 rows in set (0.00 sec)
mysql>
Fetching Data Using PHP Script
You can utilize same SQL SELECT order into PHP work mysqli_query(). This capacity is utilized to execute SQL order and later another PHP work mysqli_fetch_assoc() can be utilized to bring all the chose information. This capacity returns line as an acquainted exhibit, a numeric cluster, or both. This capacity returns FALSE if there are no more lines.
The following is a basic guide to bring records from tutorials_inf table.
Example
Evaluate the accompanying guide to show all the records from tutorials_inf table.
<html>
<head>
<title>Selecting Table in MySQLi Server</title>
</head>
<body>
<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$dbname = 'TUTORIALS';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';
$sql = 'SELECT name FROM tutorials_inf';
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
</body>
</html>
The substance of the columns are appointed to the variable $row and the qualities in line are then printed.
NOTE − never forget to put wavy sections when you need to embed a cluster esteem straightforwardly into a string.
The example yield ought to be this way −
Connected successfully
Name: ram
Name: kit
Name: abc