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
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]
Link to comment
Share on other sites

:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
:-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*
[b][color=red]THANK YOU![/color][/b]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.