What are the potential risks of not using proper syntax in MySQL queries in PHP?

Improper syntax in MySQL queries in PHP can lead to SQL injection attacks, data corruption, and unexpected behavior in your application. To prevent these risks, always use parameterized queries or prepared statements to sanitize user input and ensure proper syntax in your queries.

// Using prepared statements to prevent SQL injection
$mysqli = new mysqli("localhost", "username", "password", "database");

$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);

$username = "example";
$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    // Process the results
}

$stmt->close();
$mysqli->close();