What are the common pitfalls when using mysql_result in PHP for data retrieval and how can they be avoided?

Common pitfalls when using mysql_result in PHP include not checking if the result set is empty before trying to retrieve data, not specifying the correct row and column numbers for the data you want to retrieve, and not freeing the result set after you are done with it. To avoid these pitfalls, always check if the result set is not empty, use the correct row and column numbers, and free the result set after retrieving the data.

// Example of avoiding common pitfalls when using mysql_result
$result = mysql_query("SELECT * FROM users");

if(mysql_num_rows($result) > 0) {
    $data = mysql_result($result, 0, 'username');
    echo $data;
} else {
    echo "No data found";
}

mysql_free_result($result);