RE: [PHP-DB] Storing an array in the database

From: Date: Fri, 28 Feb 2003 16:48:25 +0000
Subject: RE: [PHP-DB] Storing an array in the database
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-26003@lists.php.net to get a copy of this message
Thanks! I checked out the php docs, serialize() returns a string containing a byte-stream representation of value that can be stored anywhere. Perfect! ---> Jonathan -----Original Message----- From: Chris Boget [mailto:chris@wild.net] Sent: Friday, February 28, 2003 10:43 AM To: jvilla@isdesigndev.com; php-db@lists.php.net Subject: Re: [PHP-DB] Storing an array in the database > 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

« previous php.db (#26003) next »