How can PHP be used to retrieve data from a database based on a selected category?
To retrieve data from a database based on a selected category in PHP, you can use SQL queries with a WHERE clause to filter the results based on the selected category. You can pass the selected category as a parameter in the SQL query to dynamically retrieve data based on the user's selection.
<?php
// Assuming you have already established a database connection
// Get the selected category from user input
$selectedCategory = $_GET['category'];
// Prepare and execute SQL query to retrieve data based on the selected category
$sql = "SELECT * FROM your_table WHERE category = :category";
$stmt = $pdo->prepare($sql);
$stmt->execute(['category' => $selectedCategory]);
// Fetch and display the retrieved data
while ($row = $stmt->fetch()) {
echo $row['column_name'];
}
?>
Related Questions
- How can PHP functions like explode and checkdate be used to manipulate date formats?
- Are there specific server-side configurations or PHP/Apache settings that could affect the functionality of headers for file downloads?
- What are the benefits of using modals in PHP for editing database entries, and how can variables be passed into modals efficiently?