How can multiple conditions be used in a SQL query in PHP?
When using multiple conditions in a SQL query in PHP, you can use the "AND" or "OR" operators to combine the conditions. This allows you to filter data based on multiple criteria in the WHERE clause of your SQL query. Make sure to properly structure your conditions and use parentheses to group them if necessary for clarity and accuracy.
// Example of using multiple conditions in a SQL query in PHP
$query = "SELECT * FROM users WHERE age > 18 AND city = 'New York'";
$result = mysqli_query($connection, $query);
// Check if the query was successful
if ($result) {
while ($row = mysqli_fetch_assoc($result)) {
// Process the retrieved data
}
} else {
echo "Error: " . mysqli_error($connection);
}
Related Questions
- Is it necessary to use functions like mysqli_real_escape_string() and htmlspecialchars() when preparing data for output as JSON in PHP?
- How can PHP be used to log IP addresses of users submitting a form and restrict multiple submissions from the same IP address?
- What are some key considerations when planning to program a forum in PHP, especially in terms of user management and forum structure?