Loop mysqli_result

女生的网名这么多〃 提交于 2021-02-15 05:27:57

问题


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 )


回答1:


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.




回答2:


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);
}



回答3:


Are you looking for this?

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


来源:https://stackoverflow.com/questions/35518629/loop-mysqli-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!