RE: Storing an array in the database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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:  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



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux