简体   繁体   中英

How to fetch all the matched rows from a Mysql table in a PHP array

I am trying to get the matched rows from a table and save it in a Global Array so that I can use it in different functions.

But when I do print_r of that array it shows only last row.

Here is my code

function setCampoFeed()
 {

        echo $sql = "SELECT campofeed.tag,campofeed.registro,campofeed.valor FROM campofeed ".
        "INNER JOIN registrofeed ON registrofeed.id = campofeed.registro ".
        "WHERE registrofeed.feed='".$this->idFeed."'";

        $result= $this->localDb->execute($sql);
        $this->campoFeed= mysql_fetch_array($result)) 

 }

So here campoFeed is the array that should have all the rows of the match, but now its just having the last row.

Thanks in advance

Use

$this->campoFeed[] = mysql_fetch_array($result);"

insted of

 $this->campoFeed= mysql_fetch_array($result);

You will get all data in array

Use this

$mergedArray=array();
while($data= mysql_fetch_array($result)) {
    $final_array = unserialize($data['data']);
    $mergedArray=array_merge($mergedArray,$final_array);
}

array_unique($mergedArray, SORT_REGULAR);

Try this one if it works for you..

$resultArray = array();
$campoFeed = array();
$resultArray = mysql_fetch_array($result);

foreach($resultArray as $key => $value){
    $campoFeed[$key] = $value;
}

print_r($campoFeed);

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