What are some best practices to follow when using the mysql_fetch_object function in PHP to avoid errors?

When using the mysql_fetch_object function in PHP, it is important to check if the result set is not empty before attempting to fetch an object. This can help avoid errors such as "Trying to get property of non-object." To do this, you can use a conditional check to ensure that the result set is not empty before fetching an object.

$result = mysql_query("SELECT * FROM table");

if(mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_object($result)) {
        // Process the fetched object here
    }
} else {
    echo "No results found.";
}