PHP SELECT Data From MySQL allows the user to select data from a database. MySQL SELECT statement allows a user to view and fetch zero or multiple rows from database tables. The SELECT statement is one of the widely used MySQL queries.
The SELECT statement returns a result that comprises rows and columns, also called a Resultset.
Read Also: PHP MySQL Multiple Insert
Index
In a PHP web application, when we need to select data from a database, the first step is to establish a connection between the PHP web application and the MySQL database. After establishing a connection, use the SELECT statement to select data from the MySQL database.
The SELECT syntax consist of two parts; first, we retrieve all columns representing full table data; second, we retrieve some specific columns from a table.
Select all columns from the table
SELECT * FROM table_name
To Select some columns of data from a table
SELECT col1, col2, col3 FROM table_name
<?php $conn = mysqli_connect("localhost", "user_name", "password", "database"); if ($conn == false) { die("ERROR: Could not connect. ".mysqli_connect_error()); } $query = "SELECT * FROM guest"; if ($result = mysqli_query($conn, $query)) { if (mysqli_num_rows($result) > 0) { echo "<table>"; echo "<tr>"; echo "<th>Firstname</th>"; echo "<th>Lastname</th>"; echo "<th>age</th>"; echo "</tr>"; while ($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>".$row['Firstname']."</td>"; echo "<td>".$row['Lastname']."</td>"; echo "<td>".$row['Age']."</td>"; echo "</tr>"; } echo "</table>"; } else { echo "No matching records are found."; } } else { echo "ERROR: Could not able to execute $query. ".mysqli_error($conn); } mysqli_close($conn); ?>
Output
Explanation
<?php $conn = new mysqli("localhost", "user_name", "password", "database"); if ($conn == false) { die("ERROR: Could not connect. ".$conn->connect_error); } $query = "SELECT * FROM Data"; if ($result = $mysqli->query($query)) { if ($result->num_rows > 0) { echo "<table>"; echo "<tr>"; echo "<th>Firstname</th>"; echo "<th>Lastname</th>"; echo "<th>Age</th>"; echo "</tr>"; while ($row = $result->fetch_array()) { echo "<tr>"; echo "<td>".$row['Firstname']."</td>"; echo "<td>".$row['Lastname']."</td>"; echo "<td>".$row['Age']."</td>"; echo "</tr>"; } echo "</table>"; $result->free(); } else { echo "No matching records are found."; } } else { echo "ERROR: Could not able to execute $query. " .$conn->error; } $mysqli->close(); ?>
Output
<?php try { $pdo = new PDO("mysql:host = localhost; dbname=database", "user_name", "password"); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die("ERROR: Could not connect. ".$e->getMessage()); } try { $query = "SELECT * FROM Data"; $result = $pdo->query($query); if ($result->rowCount() > 0) { echo "<table>"; echo "<tr>"; echo "<th>Firstname</th>"; echo "<th>Lastname</th>"; echo "<th>Age</th>"; echo "</tr>"; while ($row = $result->fetch()) { echo "<tr>"; echo "<td>".$row['Firstname']."</td>"; echo "<td>".$row['Lastname']."</td>"; echo "<td>".$row['Age']."</td>"; echo "</tr>"; } echo "</table>"; unset($result); } else { echo "No matching records are found."; } } catch (PDOException $e) { die("ERROR: Could not able to execute $query. ".$e->getMessage()); } unset($pdo); ?>
Output
Must Read: PHP MySQL WHERE Clause
The MySQL SELECT query is very helpful in working efficiently around databases. It is used to select data from one or more tables from a database. We hope you have been able to gain a clear understanding of how to work with the SELECT query.
In case, you want to learn about how to connect to MySQL using PHP and creating databases, do check out our previous blogs. Happy Learning 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…