What are the potential pitfalls of using mysql_fetch_assoc in PHP code?

Using mysql_fetch_assoc in PHP code can potentially lead to security vulnerabilities such as SQL injection attacks if the input data is not properly sanitized. To mitigate this risk, it is recommended to use prepared statements with parameterized queries when interacting with a MySQL database in PHP.

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Prepare a SQL statement with a parameterized query
$stmt = $mysqli->prepare("SELECT * FROM users WHERE username = ?");
$username = $_POST['username'];
$stmt->bind_param("s", $username);
$stmt->execute();

// Bind the result to variables
$stmt->bind_result($id, $username, $email);

// Fetch the results
while ($stmt->fetch()) {
    echo "ID: $id, Username: $username, Email: $email <br>";
}

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