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();

?>