How can PHP and MySQL be effectively used together to filter and display data based on user input, such as a selected year?
To filter and display data based on user input, such as a selected year, you can use PHP to retrieve the user input and construct a MySQL query to fetch the relevant data from the database. You can then display the filtered data on the webpage using PHP.
<?php
// Retrieve user input for selected year
$selected_year = $_POST['selected_year'];
// Connect to MySQL database
$connection = mysqli_connect('localhost', 'username', 'password', 'database_name');
// Construct MySQL query to fetch data based on selected year
$query = "SELECT * FROM table_name WHERE YEAR(date_column) = $selected_year";
$result = mysqli_query($connection, $query);
// Display filtered data
while ($row = mysqli_fetch_assoc($result)) {
echo $row['column_name'] . "<br>";
}
// Close database connection
mysqli_close($connection);
?>
Keywords
Related Questions
- In the context of PHP, how can one modify or parse JSON data with duplicate keys to ensure all values are correctly extracted and processed?
- Are there best practices for iterating through arrays to perform calculations in PHP?
- What are some important factors to consider when choosing a PHP framework for web development?