RE: [PHP-DB] Can't unserialize array from mysql
| From: | Mark Newnham | Date: | Mon, 03 Dec 2001 16:47:22 +0000 |
| Subject: | RE: [PHP-DB] Can't unserialize array from mysql | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-14770@lists.php.net to get a copy of this message | ||
Either
1. Don't addslashes before you serialize it
2. use stripslashes before you unserialize it
> -----Original Message-----
> From: Jim Doolittle [mailto:jdoolittle@mediaone.net]
> Sent: Monday, December 03, 2001 9:07 AM
> To: php-db@lists.php.net
> Subject: [PHP-DB] Can't unserialize array from mysql
>
>
> Hi,
>
> I'm trying to loop thru an unserialized array pulled from
> mysql. First I
> serialized the array before storing it:
>
> --------------------------------------------------------------
> ----------
>
> $sql ="UPDATE $table_name set performer_id='" .
> addslashes(serialize($perfor
> mer_id)) . "' WHERE lineup_id=$lineup_id";
>
> --------------------------------------------------------------
> ----------
>
> This seems to work. mysql stores $performer_id as:
>
> a:3:{i:0;s:2:"12";i:1;s:1:"1";i:2;s:1:"3";}
>
> The next time I grab $performer_id from mysql I want to
> unserialize it and
> loop thru it with foreach():
>
> --------------------------------------------------------------
> ----------
>
> $sql ="SELECT performer_id FROM lineup WHERE lineup_id=$lineup_id";
>
> $result_pid = @mysql_query($sql, $connection) or
> die("Couldn't execute query
> perfomer ID.");
> while ($row = mysql_fetch_row($result_pid)) {
> $result_pid = unserialize($result_pid);
>
> foreach ($result_pid as $performer_id) {
> (some more code here)
> }
> --------------------------------------------------------------
> ----------
>
> But this gives me an error "Warning: Invalid argument supplied for
> foreach()". I used var_dump before and after I unserialized
> just to check
> that the result set actually contained data.
>
> The first var_dump produced "array(1) { [0]=> string(44)
> "a:3:{i:0;s:2:"10";i:1;s:1:"6";i:2;s:2:"17";}" }".
> Looks
> good, but var_dump
> after unserializing gives "bool(false)".
>
> I also tried using reset() after the first var_dump, and
> stripslashes(), but
> got the same error. Can anyone see what I'm doing wrong? Thanks!
>
> Jim
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net
> For additional commands, e-mail: php-db-help@lists.php.net
> To contact the list administrators, e-mail:
> php-list-admin@lists.php.net
>