Are there any specific resources or tutorials recommended for beginners learning to display MySQL data in PHP?
One recommended resource for beginners learning to display MySQL data in PHP is the official PHP documentation on MySQLi, which provides comprehensive information and examples on how to connect to a MySQL database and retrieve data. Additionally, tutorials on websites like W3Schools or PHP.net can also be helpful for understanding the basics of displaying MySQL data in PHP.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query to retrieve data from a table
$sql = "SELECT column1, column2 FROM table_name";
$result = $conn->query($sql);
// Display the data
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Column 1: " . $row["column1"]. " - Column 2: " . $row["column2"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>