Change
if ($result->num_rows > 0) {
to
if (!empty($result) && $result->num_rows > 0) {
And then it will work.
EDIT: The reason it won't blow up is that first you are seeing if there are any results, first. If there are not, then it will not attempt to show them (this is where the error was occuring).
You really should replace
$sql =" SELECT * FROM img WHERE id = $test ";
with
$sql =" SELECT * FROM img WHERE id = ?";
and then use prepared statements. But that is another question.