How can I retrieve data from a MySQL database using PHP? Example code - Biz Tech

How can I retrieve data from a MySQL database using PHP? Example code

Listen
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabase";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// SQL query
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);

// Check if there are any rows returned
if (mysqli_num_rows($result) > 0) {
// Loop through each row
while($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . "<br>";
}
} else {
echo "No results found";
}

// Close connection
mysqli_close($conn);