Jump to content

unserialize() doesn't work .....


golem

Recommended Posts

HI ,
I try to store an array in MySQL db.
here is whay I Do:
----------------------------------------------------------------------------------------


function insertArray($sArray){

//------($sArray is an array with 3 strings: "One","Two","Three" )

//db connect stuff...
include("config.php");
//connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass);
// select the database
mysql_select_db($database);
//
//serialize the array
                $DB_array = serialize($sArray);
$user = "user1";
//update the db
$query = mysql_query("UPDATE $table SET location = '$DB_array' WHERE username = '$user'");
// get the array from db
$query = mysql_query("select location from $table where username = '$user'");
                // get the 'location' field
$field = mysql_fetch_row($query);

//if I try return here:  return $field      I get:      a:3:{i:0;s:3:"One";i:1;s:3:"Two";i:2;s:5:"Three";}
               
                //unserialize the 'location'  field
$return_array = unserialize($field);
               
                return $return_array;

  }
------------------------------------------------------
If I look in the db, the 'location' field has a serialized looking string:
a:3:{i:0;s:3:"One";i:1;s:3:"Two";i:2;s:5:"Three";}

if I return $field I get this same string, but when I try to unserialize it it fails....


I NEED HELP!!!! ???
Link to comment
https://forums.phpfreaks.com/topic/30423-unserialize-doesnt-work/
Share on other sites

using this line:

[code]$field = mysql_fetch_row($query);[/code]

you are retrieving the object that is that row.  You need to use the mysql_result function:

[code]$query = mysql_query("select location from $table where username = '$user'");
$field = mysql_result($query, 0, "location");
$return_array = unserialize($field);[/code]
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
[b][color=red]THANK YOU![/color][/b]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.