-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.php
34 lines (26 loc) · 1.09 KB
/
search.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
// In the example above, we use the mysqli class to connect to the database and retrieve the search query from the $_GET superglobal. We then perform a search using the LIKE operator to find rows in the database that contain the search query.
// Finally, we loop through the results and display them. You can customize this code to perform a search in your own database and display the results in a way that makes sense for your website.
// Connect to the database
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name');
// Check for errors
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
// Retrieve the search query
if (isset($_GET['q'])) {
$search_query = $_GET['q'];
} else {
$search_query = '';
}
// Perform the search
if ($result = $mysqli->query("SELECT * FROM table_name WHERE column_name LIKE '%$search_query%'")) {
// Loop through the results and display them
while ($row = $result->fetch_assoc()) {
echo $row['column_name'];
}
$result->free();
}
// Close the database connection
$mysqli->close();
?>