What potential pitfalls should be avoided when using mysql_query in PHP?

When using mysql_query in PHP, it is important to avoid potential SQL injection attacks by properly escaping user input. This can be done using the mysql_real_escape_string function to sanitize input before passing it to the query. Another pitfall to avoid is not checking for errors returned by mysql_query, as this can lead to unexpected behavior in your application.

// Connect to the database
$conn = mysql_connect("localhost", "username", "password");
mysql_select_db("database_name", $conn);

// Sanitize user input
$user_input = mysql_real_escape_string($_POST['user_input']);

// Perform the query
$result = mysql_query("SELECT * FROM table_name WHERE column_name = '$user_input'");

// Check for errors
if(!$result){
    die('Error: ' . mysql_error());
}

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

// Close the connection
mysql_close($conn);