What is the correct syntax for the WHERE clause in a MySQL SELECT statement in PHP?
When using a WHERE clause in a MySQL SELECT statement in PHP, the syntax should be as follows: "SELECT * FROM table_name WHERE condition". The condition should be specified based on the specific criteria you want to filter the results by. Make sure to properly format the condition with the correct operators and values to ensure the query functions as expected.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query with WHERE clause
$sql = "SELECT * FROM users WHERE age > 18";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"]. " - Age: " . $row["age"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Keywords
Related Questions
- What best practices should be followed when handling checkbox values in PHP, especially in the context of form submissions?
- What are the security considerations to keep in mind when handling user input in PHP scripts?
- How can the presence of backslashes in escaped input data be traced back to the source in PHP code and what steps can be taken to resolve this issue?