You don't have any error reporting enabled. When I run your code I see this:
PHP Notice: Trying to get property of non-object in /home/5BSpSI/prog.php on line 8
PHP Notice: Trying to get property of non-object in /home/5BSpSI/prog.php on line 9
PHP Notice: Trying to get property of non-object in /home/5BSpSI/prog.php on line 10
PHP Notice: Trying to get property of non-object in /home/5BSpSI/prog.php on line 11
Try running the code again, but add error_reporting(E_ALL);
as the first line