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

2003-02-28 Thread Hutchins, Richard
You could store it as a series of comma separated values in a text field and
manipulate it using implode() and explode() on the comma separators. I've
done this for small arrays and it works OK. WOuld like to know if there's an
easier way though. Not that this is all that bad. I'm just lazy. ;^)

HTH
Rich

 -Original Message-
 From: Jonathan Villa [mailto:[EMAIL PROTECTED]
 Sent: Friday, February 28, 2003 11:10 AM
 To: [EMAIL PROTECTED]
 Subject: [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.
 
 
 -- 
 PHP Database Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 

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



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

2003-02-28 Thread Terry Romine
just as a quick fix. I use the following snip to take an array of 
choices, enter it into a varchar field (or text if you expect alot) and 
extract back to array:
	$choicesArray is a list of checkboxes from a form
	
// put data into table
	$checkList = implode(;, $choicesArray);
	mysql_query(update table set choices='$checkList' where id=$itemID);
	...
// get data out of table
	mysql_query(select choices from table where id=$itemID);
	$choiceArray=explode(;,$row['choices']);

// this is of course, rough and simplified, but you should get the idea.

Terry

On Friday, February 28, 2003, at 10:09 AM, Jonathan Villa wrote:

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.


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


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

2003-02-28 Thread Ignatius Reilly
If your array is multidimensional, you could store it in a javascript-style:
// get the print_r()
// replace (recursively) all instances of Array( .. ) by [...]

If you ever plan to write a class or a function to do that, let me know!

Ignatius

- Original Message - 
From: Jonathan Villa [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Friday, February 28, 2003 5:09 PM
Subject: [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.
 
 
 -- 
 PHP Database Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 


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



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

2003-02-28 Thread Chris Boget
 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



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

2003-02-28 Thread Jonathan Villa
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:[EMAIL PROTECTED] 
Sent: Friday, February 28, 2003 10:43 AM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
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



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



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

2003-02-28 Thread 1LT John W. Holmes
 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.

$safe = addslashes(serialize($array));

and store $safe into a text column. Use 

$array = unserialize($database_data);

to get the array back. 

---John Holmes...

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



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

2003-02-28 Thread 1LT John W. Holmes
  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.
[snip]
   $query = INSERT INTO table
  ( field1, field2 )
  VALUES
  ( \ . serialize( $singleDimArray ) . \, \ .
serialize( $multiDimArray ) . \ );

FYI: Make sure you addslashes() _after_ you serialize your array if it can
contain quotes. Everything else remains the same.

---John Holmes...


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



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

2003-02-28 Thread Chris Boget
 FYI: Make sure you addslashes() _after_ you serialize your array if it can
 contain quotes. Everything else remains the same.

Yeah, my bad.  Good catch.

Chris


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