如何使用mysqli获取查询结果?

时间:2016-01-30 12:09:31

标签: php sql

我在CodeIgniter中有这个查询:

$user_data = $this->db
            ->select('table_users.id AS user_id, table_users.email AS user_email, table_users.GUID as user_guid, '
                    . 'table_roles.slug AS role_slug, table_user_settings.username, table_users.id_roles, '
                    . 'table_users.first_name, table_users.last_name, table_users.mobile_number, table_users.phone_number, '
                    . 'table_users.address, table_users.city, table_users.state, table_users.zip_code, table_users.notes')
            ->from('table_users')
            ->where('table_users.data', 0)
            ->join('table_roles', 'table_roles.id = table_users.id_roles', 'inner')
            ->join('table_user_settings', 'table_user_settings.GUID = table_users.GUID')
            ->where('table_user_settings.username', $username)
            ->where('table_user_settings.password', $password)
            ->get()->row_array();

    return ($user_data) ? $user_data : NULL;

我已编写此代码以进行转换:

 if($stmt = $this->db->prepare("SELECT table_users.id AS user_id, table_users.email AS user_email, table_users.GUID as user_guid, "
        . "table_roles.slug AS role_slug, table_users.id_roles, table_users.first_name, "
        . "table_users.last_name, table_users.mobile_number, table_users.phone_number, "
        . "table_users.address, table_users.city, table_users.state, table_users.zip_code, table_users.notes "
        . "FROM table_users "
        . "INNER JOIN table_roles ON table_roles.id = table_users.id_roles "
        . "INNER JOIN table_user_settings ON table_user_settings.GUID = table_users.GUID "
        . "WHERE table_users.data = 0 AND "
        . "table_user_settings.username = ? AND "
        . "table_user_settings.password = ? "))
    {
        $stmt->bind_param("ss",$username, $password);
        $stmt->bind_result($id, $email, $GUID, $slug, $id_roles, $first_name, $last_name,
            $mobile_number, $phone_number, $address, $city, $state, $zip_code, $notes);
        $result = $stmt->execute();
        $stmt->fetch();
        var_dump($stmt);
    }

    $stmt->close();
    return $result;

但我无法将一个数组中的所有变量作为第一个代码返回。 在CodeIgniter中,我可以将所有结果作为数组得到 - 例如 - > get() - > row_array()。但我不能在mysqli中做同样的事情,我只想将查询的所有选定值作为数组返回... 有人可以帮助我进行转换吗?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你想在不进行循环的情况下查询所有数组吗?

如果数据太多,查询可能需要一段时间,但您可以直接从数据库中尝试mysqli_fetch_all,如下所示(例如):

$sql = $mysqli->query("SELECT * FROM table WHERE status = '1'"); 
$results = mysqli_fetch_all($sql, MYSQLI_ASSOC); # all rows to array

另外:你需要PHP> = 5.3.0 mysqli_result :: fetch_all - mysqli_fetch_all - 将所有结果行提取为关联数组,数字数组或两者

====

编辑: 上面的方法避免了循环,但你需要自己转义值,如下所示:

$email = $mysqli->real_escape_string($email);
$GUID = $mysqli->real_escape_string($GUID);

要使用准备好的查询获取所有数组,您必须使用循环:这是mysqli的限制,这就是为什么许多人更喜欢pdo。 所以对于你的情况:

if (!$stmt->execute()) {
    // handle error
}

// Extract result set and rows
$getresult = $stmt->get_result();
while ($data = $getresult->fetch_assoc()) {
    $result[] = $data;
}

$stmt->close();
return $result;

// debug or testing
echo "<pre>";
var_dump($result);
echo "</pre>";