0

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. 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:

<?php echo $r->mydata ?>

Can anyone edit my code so it will work like before?

8
  • first of all if condition should be if($result->num_rows()>0) and remove foreach from while loop Commented Sep 14, 2014 at 15:55
  • thank for the notice, but i want to be able to echo the result like this again : <?php echo $r->mydata ?> , all what i remember is that i used a foreach loop to do that, but i forgot how :( Commented Sep 14, 2014 at 15:59
  • 2
    Without knowing what your code did before or the structure of your database table, it is almost impossible to help you. Commented Sep 14, 2014 at 16:04
  • Inside while loop u can access data as $row->mydata Commented Sep 14, 2014 at 16:04
  • i know,but i was accesing it outside the loop like this $r->mydata Commented Sep 14, 2014 at 17:27

4 Answers 4

7

it would be easier to use

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

rather than looping through all the rows and building an array.

Sign up to request clarification or add additional context in comments.

1 Comment

I just looked at your edit, you're looping through all the results and repeatedly overwriting $r with the rows one after another. The result will leave you with the last row only in $r and its a very inefficient way of achieving it $rows=$result->fetch_all(MYSQLI_ASSOC); $r=$rows[count($rows)-1] would do exactly the same thing in far fewer cpu cycles
1

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:

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

1 Comment

i want to access it outside the loop because i need that, making a social network :)
1

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

Comments

-5
$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:

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

2 Comments

Sorry for the down vote but your answer is absurd, you're double looping through all the rows and all you end with up for your efforts is the last row stored in $r
So I'm trying to use the above code to check for records from one DB and then for each row insert the record on to another. But from some strange reason there are no errors but no insert either. when I echo the results onto the page they are showing. So what could I be doing wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.