How can you filter specific data from a MySQL database using PHP?
To filter specific data from a MySQL database using PHP, you can use SQL queries with conditions to retrieve only the desired data. By using the WHERE clause in your SQL query, you can specify the criteria for filtering the data based on specific columns or values.
<?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);
}
// SQL query to select specific data based on a condition
$sql = "SELECT * FROM table_name WHERE column_name = 'desired_value'";
$result = $conn->query($sql);
// Output the filtered data
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Column 1: " . $row["column1_name"]. " - Column 2: " . $row["column2_name"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>