How can PHP be used to search a website for records outputted using PHP from a database?
To search a website for records outputted using PHP from a database, you can create a search form where users can input keywords. Then, use PHP to retrieve the search query from the form and perform a database query to fetch records that match the search criteria. Finally, display the search results on the website.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get search query from form
$search_query = $_GET['search'];
// Perform database query
$sql = "SELECT * FROM records WHERE column_name LIKE '%$search_query%'";
$result = $conn->query($sql);
// Display search results
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "No results found";
}
// Close database connection
$conn->close();
?>