Skip to content
PHP Logo

How can a centralized search engine be built in PHP to handle customizable queries across multiple tables?

Building a centralized search engine in PHP to handle customizable queries across multiple tables can be achieved by using SQL queries with JOIN statements to combine data from different tables based on user input. The search functionality can be implemented by dynamically constructing SQL queries based on the user's search criteria and executing them to retrieve relevant data from the database.

php
// Assuming $searchTerm is the input search term from the user

// 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);
}

// Construct SQL query to search across multiple tables
$sql = "SELECT * FROM table1
        JOIN table2 ON table1.id = table2.table1_id
        WHERE table1.column LIKE '%$searchTerm%'
        OR table2.column LIKE '%$searchTerm%'";

$result = $conn->query($sql);

// Display search results
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // Display search results here
    }
} else {
    echo "No results found";
}

// Close database connection
$conn->close();