SELECT * ."mysql参数化查询-如何在没有绑定的情况下存储结果


"SELECT * ..." mysqli parametrized query - how to store results without binding?

我想将mysqli查询结果存储到数组中。

到目前为止,我的代码是这样的:
function get_nearby_users($Id, $MaxDistance, $RowLimit, $RowLimitOffset)
{
    try
    {
    $query = "SELECT 
                    others.*,
                    Distance(me.Latitude, me.Longitude, others.Latitude, others.Longitude) as Distance
                from 
                    Users me
                join
                    Users others
                where 
                    me.Id = ? 
                and 
                    others.Id != ?
                having Distance < ?
                limit ?,?
        ";
        $stmt = $this->link->prepare($query);
        $stmt->bind_param('iiiii', $Id, $Id, $MaxDistance, $RowLimitOffset, $RowLimit);
        $stmt->execute();
        // how to fill $rows_array?
    }
    catch(Exception $exc)
    {
        // ...
    }
    return $rows_array;
}

我怎么能把我的结果数组当我的SQL包含像"SELECT *"的东西?

所有带有参数化查询的教程都使用bind_result函数,但我不想为所有字段创建变量并绑定它们。没有别的办法了吗?

不需要使用bind_result来存储记录集。

使用fetchAll()将结果集行存储为数组

$rows_array = $stmt->fetchAll();

我修改了你的代码。使用:

function get_nearby_users($Id, $MaxDistance, $RowLimit, $RowLimitOffset)
{
    try
    {
    $query = "SELECT 
                    others.*,
                    Distance(me.Latitude, me.Longitude, others.Latitude, others.Longitude) as Distance
                from 
                    Users me
                join
                    Users others
                where 
                    me.Id = ? 
                and 
                    others.Id != ?
                having Distance < ?
                limit ?,?
        ";
        $stmt = $this->link->prepare($query);
        $stmt->bind_param('iiiii', $Id, $Id, $MaxDistance, $RowLimitOffset, $RowLimit);
        $stmt->execute();
        $rows_array = $stmt->fetchAll(); // store result set rows as an array
    }
    catch(Exception $exc)
    {
        // ...
    }
    return $rows_array;
}