Re: Storing an array in the database
| From: | Chris Boget | Date: | Fri, 28 Feb 2003 16:43:05 +0000 |
| Subject: | Re: Storing an array in the database | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-26002@lists.php.net to get a copy of this message | ||
> How would I store an array in the database? I want to store 2 things.
> One array of shirt sizes and one array of which holds other arrays.
Easy.
<script language="php">
$singleDimArray = array( 1, 2, 3, 4, 5 );
$multiDimArray = array( array( "this" => "that", "here" =>
"there" ),
array( "near" => "far", "wherever"
=> "you are" )); // hehe
$query = "INSERT INTO table
( field1, field2 )
VALUES
( \"" . serialize( $singleDimArray ) . "\", \"" .
serialize( $multiDimArray ) . "\" )";
$result = mysql_query( $query );
$selectQuery = "SELECT * FROM table";
$result = mysql_query( $selectQuery );
$fetchedRecord = mysql_fetch_assoc( $result );
$singleDimArray = unserialize( $fetchedRecord['field1'] );
$multiDimArray = unserialize( $fetchedRecord['field2'] );
</script>
serialize() is your friend when it comes to storing arrays in
a DB field.
Chris