What common syntax errors can occur when using PHP to interact with a MySQL database?

One common syntax error when interacting with a MySQL database using PHP is not properly escaping strings in SQL queries, which can lead to SQL injection attacks. To solve this issue, you should use prepared statements or parameterized queries to safely pass user input to the database.

// Incorrect way without escaping strings
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysqli_query($connection, $query);

// Correct way using prepared statements
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username=? AND password=?";
$stmt = $connection->prepare($query);
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();