0

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

6
  • while loop can solve your problem Commented Mar 20, 2015 at 6:51
  • @vivek you mean something like this while($row= mysql_fetch_array($result)) { $this->campoFeed[]=$row; } Commented Mar 20, 2015 at 6:54
  • 1
    mysql_fetch_array needs while loop to fetch. Also I don't think $this->campoFeed or $this->campoFeed[] can actually push data into array. I would suggest setup a array like $resultArray = array();. When inside while loop, $resultArray[] = $row;. Finally $this->compoFeed = $resultArray;. Commented Mar 20, 2015 at 6:54
  • @AkiEru actually $this->campoFeed is already set as array in the beginning Commented Mar 20, 2015 at 6:56
  • 1
    If $this->campoFeed[] works, you can use while($row = mysql_fetch_array($result)) { $this->campoFeed[] = $row; } instead of creating a new array. Commented Mar 20, 2015 at 6:59

3 Answers 3

1

Use

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

insted of

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

You will get all data in array

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

1 Comment

sorry bro just tested again , its the same won't work shows only the last row
0

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

Comments

0

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

Comments

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.