3

suppose we have a mysql table "table" with fields "user" "siblings" and we want to store this family info for every user.

is it possible to do this:

$user=35;
$sibs=array("george","christina");

mysql_query("insert into table (user,siblings) values ('$user','$sibs')");

so that field "siblings" contains an array?

1

5 Answers 5

3

You want to use PHP's serialize() function for this. To reverse the process, use unserialize()

Snippit from PHP.net:

<?php
// $session_data contains a multi-dimensional array with session
// information for the current user.  We use serialize() to store
// it in a database at the end of the request.

$conn = odbc_connect("webdb", "php", "chicken");
$stmt = odbc_prepare($conn,
      "UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']);
if (!odbc_execute($stmt, $sqldata)) {
    $stmt = odbc_prepare($conn,
     "INSERT INTO sessions (id, data) VALUES(?, ?)");
    if (!odbc_execute($stmt, $sqldata)) {
        /* Something went wrong.. */
    }
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

i'll try this, i wasn't familiar with serialize to be honest
3

To build on @David Houde's answer from the post he refered to also use compression to save space (if useful to you):

<?php
mySerialize( $obj ) {
   return base64_encode(gzcompress(serialize($obj)));
}

myUnserialize( $txt ) {
   return unserialize(gzuncompress(base64_decode($txt)));
}
?>

Comments

2

You can use serialize https://www.php.net/manual/en/function.serialize.php

But it's really bad practice.

3 Comments

i usually prefer implode but in this occasion, which is much more complicated than the example i've given, won't work
Can you say why serialize is bad practice?
@pavium Because 1) if you need to store key-val pairs, then Mysql is not the best choice 2) if in future you'll want to select all items, who has one (or more) properties from list, then you'll be in a big trouble.
2

You can use implode() to add a divider between values. For example, you can use the pipe "|"

e.g.

$user=35;
$sibs=array("george","christina");

$sibs1 = ''.implode('|',$sibs).'|';

mysql_query("insert into table (user,siblings) values ('$user','$sibs1')");

When extracting information from the database use LIKE '%|$var|%' in your query to extaract the exact value. e.g. |Christina|

Comments

0

Yes, you can serialize() the array prior to inserting it, or implode() it with some kind of a separator of your choice for this, but I would recommend you just insert separate rows for each user->sibling relationship.

2 Comments

i thought about implode, and i usually use this. but in fact the example i've given here is much more complicated than the real problem i have to encounter. didn't know about serialize, i'll give it a shot
I'm sure you'd get a much better solution than if you just post the problem that you're trying to solve. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.