Vikram Anand Bhushan
Vikram Anand Bhushan

Reputation: 4886

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

Upvotes: 0

Views: 119

Answers (3)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

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

Upvotes: 0

I'm Geeker
I'm Geeker

Reputation: 4637

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

Upvotes: 0

Ishan Shah
Ishan Shah

Reputation: 1676

Use

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

insted of

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

You will get all data in array

Upvotes: 1

Related Questions