简体   繁体   中英

How to get query result with mysqli?

I have this query in 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;

I have write this code for convert it:

 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;

but I can't return all variable in one array as the first code. In CodeIgniter I can get all result as array like ->get()->row_array(). But I can't do the same in mysqli, I just want return all the selected value of the query as array... Someone could help me on this conversion?

If I understand correctly, you want to query all to an array without doing a loop?

The query might take a while if it's too much data, but you can try mysqli_fetch_all directly from database, like this (example):

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

Also: you need PHP >= 5.3.0 mysqli_result::fetch_all -- mysqli_fetch_all — Fetches all result rows as an associative array, a numeric array, or both

====

EDIT: The method above avoids the loop, but you need to escape the values on your own, like this:

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

To get all to an array using a prepared query, you will have to use a loop: This is a limitation of mysqli and that's why many people prefer pdo. So for your case:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM