DomingoSL
DomingoSL

Reputation: 15484

From mysqli_result Object to string

I have a mysqli_result Object doing this:

    $mysqli->real_query("SELECT * FROM users WHERE `level` > 2");
    $user_list = $mysqli->use_result();
    print_r($user_list); die();

Doing so, the print_r give me:

mysqli_result Object ( [current_field] => 0 [field_count] => 6 [lengths] => [num_rows] => 0 [type] => 1 )

Later im just using while ($row = $user_list->fetch_assoc()) to parse all the content. The think is before doing the while I want to retrieve the num_rows from the mysqli_result Object but doing just $user_list['num_rows'] does not work:

Fatal error: Cannot use object of type mysqli_result as array

Upvotes: 1

Views: 10162

Answers (4)

Lackeeee
Lackeeee

Reputation: 459

Try this, in my case it works :)

$query  = "SELECT * FROM users WHERE `level` > 2";
$result = mysqli_query($connection, $query);
while ($rows = mysqli_fetch_array($result, MYSQLI_NUM)) {
     var_dump($rows);
}

Upvotes: 1

Nanhe Kumar
Nanhe Kumar

Reputation: 16297

$mysqli->real_query("SELECT * FROM users WHERE `level` > 2");
$user_list = $mysqli->use_result();
echo $user_list->num_rows;

Upvotes: 0

Vinoth Babu
Vinoth Babu

Reputation: 6852

You have to get the num_rows using the $result->num_rows

if ($result = $mysqli->query("")) {

    /* determine number of rows result set */
    $row_cnt = $result->num_rows;
}

After getting num_rows you can fetch it.

Upvotes: 1

SteveUK
SteveUK

Reputation: 161

Does $user_list->num_rows work?

Upvotes: 3

Related Questions