Here’s a simple example of how to interact with a MySQL database using PHP to display records on a webpage:
<?php
// Create connection:
$conn = new mysqli ("server", "user", "password", "database");
mysqli_set_charset ($conn, "utf8");
// Check connection:
if ($conn -> connect_error) {
die ("Connection failed: " . $conn->connect_error);
}
// SQL query:
$sql = "SELECT * FROM top_5000_songs LIMIT 0, 10";
$result = $conn -> query($sql);
if ($result -> num_rows > 0) {
// Output each row
while ($row = $result -> fetch_assoc()) {
echo "<div>" . $row["song_title"] . " - " . $row["artist"] . "</div>";
}
}
else {
echo "No records found";
}
$conn -> close();
?>
SELECT
: The keyword used to query data.*
: Represents all columns in the table (you can specify specific columns instead, such as column_01
, column_02
).FROM top_5000_songs
: The table name from which to retrieve the records.