What potential pitfalls could arise when using PHP to query data from a MySQL database?

One potential pitfall when using PHP to query data from a MySQL database is the risk of SQL injection attacks if user input is not properly sanitized. To prevent this, always use prepared statements with bound parameters to securely handle user input and prevent malicious SQL queries.

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

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

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

// Execute the prepared statement
$stmt->execute();

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

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

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