What is the purpose of using mysql_fetch_array() in PHP and what potential pitfalls should be considered when using it?

The purpose of using mysql_fetch_array() in PHP is to retrieve a row of data from a MySQL database result set as an associative array or numeric array. When using mysql_fetch_array(), it's important to be aware of potential pitfalls such as the deprecated nature of the function in newer PHP versions and the vulnerability to SQL injection attacks if input is not properly sanitized.

// Example of using mysqli_fetch_array() instead of mysql_fetch_array()

// Connect to MySQL database
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Query database
$result = mysqli_query($conn, "SELECT * FROM users");

// Fetch data as associative array
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    // Process data
}

// Close connection
mysqli_close($conn);