Why is it necessary to enclose 'Waleb' in single quotes in the WHERE clause of a MySQL query in PHP?
Enclosing 'Waleb' in single quotes in the WHERE clause of a MySQL query in PHP is necessary because SQL syntax requires string values to be enclosed in quotes. Without the quotes, the query will not be able to properly identify 'Waleb' as a string and may result in a syntax error. By including the single quotes, the query will correctly interpret 'Waleb' as a string value to be compared in the WHERE clause.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Prepare and execute a SQL query with 'Waleb' enclosed in single quotes
$sql = "SELECT * FROM table_name WHERE column_name = 'Waleb'";
$result = $conn->query($sql);
// Process the query result
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Column Value: " . $row["column_name"];
}
} else {
echo "0 results";
}
// Close the connection
$conn->close();
?>