Php “选择*…”mysqli参数化查询-如何在不绑定的情况下存储结果?

Php “选择*…”mysqli参数化查询-如何在不绑定的情况下存储结果?,php,mysqli,Php,Mysqli,我想将mysqli查询结果存储到数组中 到目前为止,我的代码如下所示: function get_nearby_users($Id, $MaxDistance, $RowLimit, $RowLimitOffset) { try { $query = "SELECT others.*, Distance(me.Latitude, me.Longitude, others.Latitude,

我想将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\u result来存储记录集

仅用于将结果集行存储为数组

$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;

}

FWIW,如果通过PDO而不是mysqli使用MySql,则不需要这样做。您还可以获得许多其他附加好处,如下所述: