Your database object $conn
is out of scope in the rand_id()
function. You could pass it as an argument:
function rand_id($conn)
{
// ...
}
Or use the global
method:
function rand_id()
{
global $conn;
// $conn is now accessible
}
More info about Variable Scope on the Manual.
For future debugging, remember to check your error log. In this case you should have something like:
Fatal Error: Call to a member function query() on a non-object in ...