What potential pitfalls should be avoided when using raw mysql_query/pg_query functions in PHP?

When using raw mysql_query/pg_query functions in PHP, potential pitfalls to avoid include SQL injection attacks and lack of error handling. To mitigate these risks, it is recommended to use parameterized queries and properly handle any errors that may occur during the query execution.

// Example of using parameterized queries with mysqli
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

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

// Set parameters and execute
$username = "john_doe";
$stmt->execute();

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

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

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