Search
SELECT * FROM products WHERE name LIKE '%search_term%';
<form method="GET" action="search.php">
<input type="text" name="query" placeholder="Search...">
<button type="submit">Search</button>
</form><?php
// Get the search query from the form submission
$query = $_GET['query'];
// Connect to the database
$host = 'localhost';
$user = 'yourusername';
$pass = 'yourpassword';
$dbname = 'yourdatabasename';
$conn = new mysqli($host, $user, $pass, $dbname);
// Build the SQL query
$sql = "SELECT * FROM products WHERE name LIKE '%$query%'";
// Execute the query
$result = $conn->query($sql);
// Display the search results
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<h2>" . $row['name'] . "</h2>";
echo "<p>" . $row['description'] . "</p>";
echo "<p>$" . $row['price'] . "</p>";
echo "<hr>";
}
} else {
echo "No results found for '$query'";
}
// Close the database connection
$conn->close();
?>
phpLast updated