tags:

views:

71

answers:

3

I need to insert mutiple data in the field and then to retrieve it as an array. For example I need to insert "99999" into table item_details , field item_number, and the following data into field bidders associated with item_number : userx usery userz Can you please let me know what sql query should I use to insert the info and what query to retrieve it ? I know that this may be a silly question but I just can't figure it out .

thank you in advance, Michael .

A: 

implode() the array (hence serialising it) then pass that string into MySQL.

Delan Azabani
I think that implode is not very good practice. Any idea how union works ?
Michael
A: 

Before stored in sql, convert the array into string by using implode. After the retrive the values of string into array by using explode function. For storing the string use varchar or text as field type.

Karthik
+1  A: 

If you are simply wanting to store an array in a MySQL Field for later retrieval, then you could use implode()[PHP Docs] as suggested above (which will destroy the array's keys, but retain the array's values), or serialize()[PHP Docs] which will retain both the values and the associated keys.

$theArray = array(
  'key1' => 'One' ,
  'key2' => 'Two'
);
$serArray = serialize( $theArray ); // a:2:{s:4:"key1";s:3:"One";s:4:"key2";s:3:"Two";}
$sqlStr = 'INSERT INTO `table` ( `name` , `arrayField` ) VALUES ( "Test Row" , "'.$serArray.'" )';

If you are talking about using two related table to store data, then you are probably best advised to refer to tutorials like http://www.sql-tutorial.net/SQL-JOIN.asp, http://www.databasejournal.com/features/oracle/article.php/3527921/Just-SQL-Part-IV--Joining-Tables.htm

Lucanos