Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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 .

share|improve this question
    
Your question is not clear, you should provide more details about your tables too. –  Sarfraz May 21 '10 at 7:34
    
table = item_details ------------------ fields : item_number, bidders ------------------ data to insert : 99999 into item_number ---------- userx, usery, userz into bidders –  Michael May 21 '10 at 7:48

3 Answers 3

up vote 1 down vote accepted

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

share|improve this answer

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

share|improve this answer
    
I think that implode is not very good practice. Any idea how union works ? –  Michael May 21 '10 at 15:09

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.