Asked 3 years ago
9 Jun 2021
Views 358
Mahesh Radadiya

Mahesh Radadiya posted

query to search through an entire database in mysqli

query to search through an entire database in mysqli
kiran

kiran
answered Feb 28 '23 00:00

To search through an entire database in mysqli, you can use a SELECT statement with the LIKE operator . Here's an example:



<?php
// Connect to the database
$conn = mysqli_connect("localhost", "username", "password", "database_name");

// Define the search term
$search_term = "apple";

// Construct the query
$query = "SELECT * FROM `table_name` WHERE CONCAT_WS(' ', `column1`, `column2`, `column3`) LIKE '%$search_term%'";

// Execute the query
$result = mysqli_query($conn, $query);

// Check if there are any results
if(mysqli_num_rows($result) > 0) {
    // Loop through the results and display them
    while($row = mysqli_fetch_assoc($result)) {
        echo $row['column1'] . " " . $row['column2'] . " " . $row['column3'] . "<br>";
    }
} else {
    echo "No results found.";
}

// Close the connection
mysqli_close($conn);
?>


In this example, we first connect to the database using mysqli_connect(). We then define the search term and construct the query using the CONCAT_WS function to combine multiple columns into one string to search. We use the LIKE operator with wildcard characters (%) to search for the search term in any part of the concatenated string . We then execute the query using mysqli_query() and loop through the results using mysqli_fetch_assoc(). Finally, we close the connection using mysqli_close().
Post Answer