问题
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