What is the purpose of the PHP script for the guestbook?
The purpose of the PHP script for the guestbook is to allow users to leave comments or messages on a website. The script typically collects user input, stores it in a database, and displays the comments on the website for others to see.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "guestbook";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve comments from the database
$sql = "SELECT * FROM comments";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"]. " - Comment: " . $row["comment"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>