The SELECT statement returns a result that comprises of rows and columns, also called as a Result Set
.
The SELECT statement has several clauses:
SELECT
selects that table columns in which the data to be inserted.FROM
denotes the table from which the data is retrieved.JOIN
retrieves data from various tables depending on join conditions.WHERE
clause applies filters to rows to grab the data.GROUP BY
brings the rows together to apply combination functions on each cluster.HAVING
filters group depending on groups defined by GROUP BY clause defined.ORDER BY
defines result set order that is returend.LIMIT
obliges different rows that are returned.[php]
<!DOCTYPE html>
<html>
<body>
<?php
$HostName = "localhost";
$UserName = "root";
$Password = "password";
$dbname = "accounts";
// Create connection
$connection = new mysqli($HostName, $UserName, $Password, $dbname);
// Check connection
if ($connection->connect_error)
{
die("Connection failed: " . $connection->connect_error);
}
$sql = "SELECT id, First, Last FROM users2";
$result = $connection->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "id: " . $row["id"]. " – Name: " . $row["First"]. " " . $row["Last"]. "<br>";
}
}
else
{
echo "0 results";
}
$connection->close();
?>
</body>
</html>
[/php]
The output looks like below in phpmyadmin with all the selected column values.
MySQLi (Procedural)
[php]
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "Student";
// Create connection
$connection = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
//Selecting the details
$sql = "SELECT id, First, Last FROM users2";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result) > 0)
{
// output data of each row
while($row = mysqli_fetch_assoc($result))
{
echo "id: " . $row["id"]. " – Name: " . $row["First"]. " " . $row["Last"]. "<br>";
}
}
else
{
echo "0 results";
}
mysqli_close($connection);
?>
</body>
</html>
[/php]