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 are the potential pitfalls of using arrays with specific indexes (0 and 1) for replacing smilies in PHP code?
- How can PHP developers securely handle user authentication when integrating login functionality from an external source?
- What is the correct file extension and syntax to save a PHP script for detecting browser types?