What are the potential pitfalls of using @mysql_fetch_assoc() in PHP and how can they be avoided?

Using `mysql_fetch_assoc()` in PHP can lead to potential pitfalls such as deprecated function usage and vulnerability to SQL injection attacks. To avoid these issues, it is recommended to use the improved `mysqli_fetch_assoc()` function or PDO for database operations in PHP.

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

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

// Query the database using mysqli
$result = $mysqli->query("SELECT * FROM table");

// Fetch results using mysqli_fetch_assoc()
while ($row = $result->fetch_assoc()) {
    // Process the data
}

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