How can PHP be used to filter and display specific data based on certain criteria in a database?
To filter and display specific data based on certain criteria in a database using PHP, you can use SQL queries with conditions to retrieve only the data that meets the specified criteria. You can then use PHP to fetch and display the filtered data on your webpage.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Define the criteria
$criteria = "some_criteria";
// SQL query to retrieve data based on criteria
$sql = "SELECT * FROM table_name WHERE column_name = '$criteria'";
$result = $conn->query($sql);
// Display the filtered data
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Column 1: " . $row["column1"] . " - Column 2: " . $row["column2"] . "<br>";
}
} else {
echo "0 results";
}
// Close the connection
$conn->close();
?>