Yep, simple fix for that...
The line $average = $row[rating]; should read: $average = $row["AVG(rating)"];
The return column name is actualy the exact string that you used in the select part of the statement.
Examples:
select test1,test2,test3,sum(test1) from tablename
then you would have resulted in the following avaliable values in your $row variable.
$row["test1"]
$row["test2"]
$row["test3"]
$row["sum(test1)"]
Easy ehe... you wouldn't belive how long it took me to find a simple tutorial to show me how this worked when I had to learn it for the first time 🙂