2015-02-06 132 views
2

看看是否有简单的方法从函数中的数据库查询中倾出我的结果。从函数返回数据库数组

功能:

function getAllCustomers() { 
    global $mysqli; 
    $query = $mysqli->query("SELECT * FROM customers"); 
    return $query; 
} 

这是一类,所以:

$customer = new Customer(); 
print_r($customer->getAllCustomers()); 

这说明:

mysqli_result Object ([current_field] => 0 [field_count] => 10 [lengths] => [num_rows] => 24 [type] => 0) 

但我希望返回所有的数组所有行的字段和值。

我知道我可以运行一个foreach,但不知道是否有一个更简单的方法?

+0

为['mysqli_result'(http://php.net/manual/en/class.mysqli-result.php),其显然你是从函数返回的,你应该能够自己回答这个问题。 – Jon 2015-02-06 15:01:42

回答

0

query的结果不是数据本身,而是mysql_result对象。您可以使用它的方法fetch_all:如果你看看文档

function getAllCustomers() { 
    global $mysqli; 
    $result = $mysqli->query("SELECT * FROM customers"); 
    return $result->fetch_all(); 
}