Wes
Wes

Reputation: 856

Loop mysqli_result

I can't seem to figure out how to loop through the rows in this object array. For example, how would I echo the value in each row?

$sql = "SELECT my_ids FROM manager WHERE is_live = 0";
$result = $db->query($sql);

When I print_r($result); I get

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

Upvotes: 1

Views: 676

Answers (3)

jameshwart lopez
jameshwart lopez

Reputation: 3161

Make sure your are connected to the database. Example mysqli connection below.

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "databasename";
$db = new mysqli($hostname, $username, $password, $database);

What you need is to fetch the data on your query and loop through on it. Like this

$sql = "SELECT my_ids FROM manager WHERE is_live = 0";
$result = $db->query($sql);

 while ($manager_row = $result->fetch_assoc()) {
        echo $manager_row ['my_ids'];
       echo '<pre>'; print_r($manager_row);echo '</pre>';
 }

You can also use fetch_all(). Like this

$sql = "SELECT my_ids FROM manager WHERE is_live = 0";
$result = $db->query($sql);
$all_results = $result->fetch_all();

foreach($all_resuls as $data){
    print_r($data);
}

Upvotes: 1

Gokul Shinde
Gokul Shinde

Reputation: 965

Try to loop on $result with foreach loop:

<?php

foreach($result as $key => $val)
{
  echo "key is=> ".$key." and Value is=>".$val;
}

Keys will be current_field field_count etc.

Upvotes: 2

Niklesh Raut
Niklesh Raut

Reputation: 34924

Are you looking for this?

 while($row = mysqli_fetch_array($result)){
      echo $row['my_ids'].'<br />';
 }

Upvotes: 0

Related Questions