Moroianu Alexandru
Moroianu Alexandru

Reputation: 167

PHP Returning a value from and use methods on it

So , i have this function which returns a result set :

function get_products()
{
    global $database;
    $statement = 'select * from products';
    return $result = $database->query($statement);
}

and i want to call it in other php file like this :

include './DBvalidation.php';
   $res = get_products();
   $res->fetchAll();

but the method fetchAll() of the return set is not recognized there . what should i modify ?

Upvotes: 0

Views: 42

Answers (2)

Uriziel
Uriziel

Reputation: 177

You cant just use the $result assosiation as return statement (it will just return true in Your case, which is wrong) use something like this instead :

return $database->query($statement);

You dont even need the $result variable at all anyway cause its end of function execution and You wont be able to use it elsewhere.

Upvotes: 1

t.h3ads
t.h3ads

Reputation: 1878

function get_products()
{
    global $database;
    $statement = 'select * from products';
    return $database->query($statement);
}

OR:

function get_products()
{
    global $database;
    $statement = 'select * from products';
    $result = $database->query($statement);
    return $result;
}

Upvotes: 1

Related Questions