What potential pitfalls should be considered when modifying MySQL queries in PHP?

When modifying MySQL queries in PHP, it is important to be cautious of SQL injection attacks. To prevent this, it is recommended to use prepared statements with parameterized queries. This helps sanitize user input and prevents malicious code from being executed.

// Example of using prepared statements to prevent SQL injection

// Establish a connection to the database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Prepare a SQL query with a placeholder for the user input
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");

// Bind the user input to the placeholder
$stmt->bind_param("s", $username);

// Set the user input
$username = "john_doe";

// Execute the query
$stmt->execute();

// Fetch the results
$result = $stmt->get_result();

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

// Close the statement and connection
$stmt->close();
$mysqli->close();