serialize with UPDATE query

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
hi all,

How do  I use serialize with MySQL's UPDATE query?

I have an array, $x = (1, 5, 9).

My MySQL table:

id      x_id     y_id
---     -------    --------
1        1         5
2        1         4
3        2         5
4        1         2

I would like to update the table with values of array $x where x_id ==
1. How do I achieve this using serialize()?  

Thank you!!

Re: serialize with UPDATE query wrote:

Quoted text here. Click to load it

Hi Ben,

I am confused.
which column do you want to update?
And what values should it be?

Do you want to store a serialized array in some column and cannot figure out  
the syntax?

Here is some example, but it may not be appropriate in your situation  
because I do not understand 100%.

// example:
create table tbltest (
 testid serial primary key,
 somevalue text

$x = array(1,5,9);
// store serialized array $x in column  where x_id is 1 in tbltest
$thevalue = serialize($x);
$SQL = "UPDATE tbltest SET somevalue='".$thevalue."' where testid=1;";

// go execute $SQL


Hope that helps.
If you serialize some array, the result is a string. So maybe add a function  
that makes the string (stored in $thevalue here above) safe, just to be  
sure that the serialized string contains no characters that might screw up  
the query (like ').  
In mysql that is called mysql_escape_string().

mysql_escape_string -- Escapes a string for use in a mysql_query
string mysql_escape_string ( string unescaped_string )

Hope this help.
Erwin Moller

Quoted text here. Click to load it

Site Timeline