Jump to content

write a query to insert an array with vary size each time into MySQL


Recommended Posts

Hello,

 

how do you insert an " vary size" array into a MySQL table by using a for loop??

the array has different number of item in it each time when the php file loads, therefore is there a way to write a query which is suitable for any size array, perhaps using for loop??

 

Reference:

with one fixed size array I will use:

mysql_query("INSERT INTO table VALUES ('array[0]','array[1]',.........)")

 

with vary size array, how??

 

Thank you

 

 

 

 

for ($i = 0; $i < count($array); $i++) {
// Do Insert
}

 

If you're referring to the items themselves... like one time you need to insert 3 fields, then the next time 8 fields, that is a bit tougher as you would have to keep enough columns in the table to get the largest number of array items...

serialize

 

Make the DB field a text and use that function above.

 

When retrieving it from a DB use unserialize

 

thanks but thats not related to my question :P

 

So let me get this straight. You want to loop through the array and insert the values into it?

 

Before I waste my time writing an example code you need to show me the full array structure and how it needs to be entered. Either do a print_r on the array you want to insert and paste it here or write it out on your own.

 

Providing us with what you actually want is the key to getting your questioned answered without all this going back and forth with me just needing more basic information.

ok here is an example:

 

the array:

 

$temparray[0] = "fgfdgfgd";

$temparray[1] = "aadsaddsds";

$temparray[2] = "fdgfdgd";

$temparray[3] = "dfgfgfdga";

.......

$temparray[ x ] = "ksdfjsdf";

 

x is a number which changes everytime, but I know the number every time because it related something but the number just change everytime <- the main concern.

 

so I want to insert all the items within the array into 1 row of MySQL table (do not worry about the number of field, it is exact the same as the number of items in the array so they fit)

 

so to insert into the table we use:

mysql_query("INSERT INTO table VALUES ('array[0]','array[1]',.........)")

 

but the number changes so i guess it need a for loop or other kind of method, question is how to write this query which meet the condition of this case??

 

Thanks!!

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.