简体   繁体   English

PHP mysqli查询结果与foreach循环回显

[英]PHP mysqli query result echo with foreach loop

in my last project I used foreach loop to assign to every mysqli result a variable, like $r->mydata , but I formatted my pc accidentally, so I lost my core file and I can't remember how exactly I did that. 在我的上一个项目中,我使用了foreach循环为每个mysqli结果分配一个变量,例如$r->mydata ,但是我不小心格式化了PC,所以我丢失了核心文件,我不记得自己是怎么做到的。 I remember that I did something like this 我记得我做了这样的事情

$result = $db->query("SELECT * FROM data");
if($result->num_rows){
    while ($row = $result->fetch_object()) {
        foreach ($row as $r){
            $row[] = $r;
        }
    }
}

And I can access the result from outside the while loop like this: 我可以从while循环外部访问结果,如下所示:

<?php echo $r->mydata ?>

Can anyone edit my code so it will work like before? 任何人都可以编辑我的代码,使其像以前一样工作吗?

it would be easier to use 它会更容易使用

$rows=$result->fetch_all(MYSQLI_ASSOC);

rather than looping through all the rows and building an array. 而不是遍历所有行并构建一个数组。

Maybe you don't remember how you did it, but if you did it once you should know at least which approach you followed to solve this, let help you to understand the code first: 也许您不记得自己是怎么做的,但是如果您曾经做过,那么您至少应该知道您采用哪种方法来解决此问题,让我们首先帮助您理解代码:

$result = $db->query("SELECT * FROM data");
if($result->num_rows>0){
    //iterating only if the table is not empty
    while ($row = $result->fetch_object()) {
        //Here you are iterating each row of the database
        foreach ($row as $r){
            //Here you are iterating each column as $r
            //and (trying) adding it again to the $row array
            $row[] = $r;
        }
    }
}

Why would you like to access to the $row outside the loop, if what you want to do is print each row, you can do it inside the loop: 您为什么要在循环外访问$row ,如果要打印的是每一行,则可以在循环内进行操作:

$result = $db->query("SELECT * FROM data");
if($result->num_rows>0){
    while ($row = $result->fetch_object()) {
        foreach ($row as $r){
            echo $r.'<br>';
        }
    }
}

If you do something like what follows, what part of the 'printed' data do you need? 如果您执行以下操作,则需要“打印”数据的哪一部分? You may have been 'specializing' the result set by eliminating the rows to only use a single row... 您可能已经通过消除仅使用一行的行来“专门化”结果集...

$result = $db->query("SELECT * FROM data");

$r = new stdClass();

// Only loop if there is a result.
if ($result) 
{
    $r->myData = []; // You aren't exactly clear on 'myData'.  In this instance, I am setting the rows inside of it.
    while ($row = $result->fetch_object())
    {
        $r->myData[] = $row;
    }

    $result->close(); // Free result set
}

print_r($r->myData);
$records = array();
$result = $db->query("SELECT * FROM data");
if($result->num_rows){
 while ($row = $result->fetch_object()) {
   $records[] = $row;
   foreach($records as $r);
 }  
}

and now you can access the result from any place in the page,example echo inside html h1: 现在您可以从页面中的任何位置访问结果,例如HTML h1中的echo示例:

<h1>My name is: <?php echo $r->name ?></h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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