简体   繁体   中英

How to get each value in a column of database in php

I don't have enough knowledge about this criteria:

I want to loop inside the for each loop to get all values in a particular column.

For example: I got the values from DB through get_result and store the result in $results .

After that use:

for each($results as $result)
❴
    $output = $result->message
❵

Where message is a column in DB.

I want to loop over all the messages instead of storing last one by replacing.

Can you please give me suggestions on how to loop inside for each ?

Try this:

$output[] = $result ->message;

Now $output will contain all messages on index 0, 1, 2 ...

You are facing the issue because:

$output=$result ->message;

the above line is present inside the loop, and each new iteration onerride the old value.

Well if you just looking for foreach inside foreach then you can try the following.

 <?php 
    foreach($results as $result){
        $output=$result->message;
        foreach($output as $messages){
            echo $messages;
        }
     }
 ?>

You don't need to put the message into another variable. You can do whatever you need to do inside the loop. For example, if you are displaying the messages, you can get it done inside the loop:

foreach ($results AS $result) {

   echo $results->message . "<br>";

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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