views:

219

answers:

2

I am working in PHP/MySQL.

I have a table in my database called hourly in that table their is a column named webaddress these are serialized. There are multiple rows of each column of webaddresses each is serialized.

I need to pull each row, unserailize them then put them into an array.

I tried using this bit of code but it only grabs 1 row because of the limitations of the PHP functions.

while ($row = mysql_fetch_array($results)) {$test = unserialize($row[0]);}

I was thinking something like this might work:

while(($row = mysql_fetch_array($results)) !== FALSE) {$test[] = $row;}

That didn't work ...

How can I grab each row, then unserailize it then add it to an array? I just need the data in the web_addresses field currently there is 3 rows of data. So there would be 3 serialized arrays in each web_addresses field that I need to unserialize and combine into another array. Hopefully that makes more sense.


Here is the MySQL table:

CREATE TABLE `hourly` (
  `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `id` bigint(20) NOT NULL auto_increment,
  `month` longtext NOT NULL,
  `day` longtext NOT NULL,
  `year` longtext NOT NULL,
  `source` longtext NOT NULL,
  `web_address` longtext NOT NULL,
  `at_replies` longtext NOT NULL,
  `words` longtext NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1
A: 

change it to $test[] = unserialize($row[0]).

It will unserialize your data into an array, and then push that array into the $test array. To see how it looks, after your loop, add this line:

print_r($test);

It will be something like this:

array(
    [0] => array(
        // ... the first record's data
    ),
    [1] => array(
        // ... the second record's data
    ),
    // etc
)
nickf
+1  A: 

EDIT: Now reflects updates based on info by the OP:

From your question it sounds like each row has one serialized column, and that column contains a serialized array of three items per rows. So this should work:

$collection = array();
while ( $row = mysql_fetch_array($results)) {
    $values = unserialize($row[0]);
    # $values has unserialized the data into its own array with 3 items

    $collection = array_merge($collection, $values);
}

If there were three db rows, and each field had a serialized array with three items, $collection now contains an array with 9 items.

Doug Neiner
My edits might help provide some more info. I think your on the right track ...
Adam Evers
That worked perfectly.Thanks!
Adam Evers