How can PHP be used to search for actors, movies, or directors in a film database?
To search for actors, movies, or directors in a film database using PHP, you can utilize SQL queries to retrieve the relevant information based on user input. You can use the SQL SELECT statement with WHERE clause to filter the results based on the search term provided by the user. Additionally, you can use PHP variables to store the search term and dynamically construct the SQL query to fetch the desired data.
<?php
// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "film_database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the search term from user input
$searchTerm = $_GET['search'];
// Construct the SQL query to search for actors, movies, or directors
$sql = "SELECT * FROM films WHERE actor LIKE '%$searchTerm%' OR movie LIKE '%$searchTerm%' OR director LIKE '%$searchTerm%'";
// Execute the query
$result = $conn->query($sql);
// Display the search results
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Actor: " . $row["actor"]. " - Movie: " . $row["movie"]. " - Director: " . $row["director"]. "<br>";
}
} else {
echo "No results found";
}
// Close the database connection
$conn->close();
?>
Related Questions
- How do you typically store paths and categories for individual images in a MySQL database when showcasing photos on a website?
- How can one ensure that there are no remnants or dependencies left from PHP 5 after installing PHP 7?
- What are the potential pitfalls of using absolute paths for including PHP files?