In the following code, the first print_r inside the while loop prints different story content. The problem I am having is that the 2nd print_r statement produces the exact same story over and over from the $stories array.
$stories = array();
while($row = mysql_fetch_array($result)){
$story->name = $row['Name'];
...
$story->date = $row['Date'];
print_r($story); //for testing
array_push ( $stories , $story );
}
print_r($stories);
Edit:
someone asked for command line output, but this is a hosted account. In case the above is not clear though:
From inside loop:
(
[id] => 9370
[name] => Five Below, Inc.
...
)
stdClass Object
(
[id] => 9362
[name] => Peregrine Pharmaceuticals Inc.
...
)
stdClass Object
(
[id] => 9363
[name] => Mitel Networks Corporation
)
...
stdClass Object
(
[id] => 9370
[name] => Five Below, Inc.
...
)
After loop:
Array
(
[0] => stdClass Object
(
[id] => 9370
[name] => Five Below, Inc.
...
)
[1] => stdClass Object
(
[id] => 9370
[name] => Five Below, Inc.
)
[2] => stdClass Object
(
[id] => 9370
[name] => Five Below, Inc.
)
It seems the problem lies in
$story. I made test case code:produces
which is wrong. However adding
unset($story);and have new object create each time, solves the issue:gives correct: