The Read operation is the second step in CRUD (Create, Read, Update, Delete). It retrieves data from a MySQL database and displays it using PHP with SELECT queries and extensions like MySQLi or PDO.
// Read user records from MySQL database
<?php
$conn = new mysqli("localhost","username","password","myDB");
if ($conn->connect_error) {
die("Connection failed");
}
$sql = "SELECT id, first_name, last_name, email FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["first_name"]." ".$row["last_name"]."<br>";
}
}
$conn->close();
?>