phppaper Posted December 27, 2008 Share Posted December 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138559-write-a-query-to-insert-an-array-with-vary-size-each-time-into-mysql/ Share on other sites More sharing options...
MatthewJ Posted December 27, 2008 Share Posted December 27, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/138559-write-a-query-to-insert-an-array-with-vary-size-each-time-into-mysql/#findComment-724460 Share on other sites More sharing options...
phppaper Posted December 27, 2008 Author Share Posted December 27, 2008 thanks! Thats not going to work, instead of inserting into one row, it will insert into different row is it?? Quote Link to comment https://forums.phpfreaks.com/topic/138559-write-a-query-to-insert-an-array-with-vary-size-each-time-into-mysql/#findComment-724461 Share on other sites More sharing options...
premiso Posted December 27, 2008 Share Posted December 27, 2008 serialize Make the DB field a text and use that function above. When retrieving it from a DB use unserialize Quote Link to comment https://forums.phpfreaks.com/topic/138559-write-a-query-to-insert-an-array-with-vary-size-each-time-into-mysql/#findComment-724464 Share on other sites More sharing options...
phppaper Posted December 27, 2008 Author Share Posted December 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138559-write-a-query-to-insert-an-array-with-vary-size-each-time-into-mysql/#findComment-724466 Share on other sites More sharing options...
premiso Posted December 27, 2008 Share Posted December 27, 2008 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 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. Quote Link to comment https://forums.phpfreaks.com/topic/138559-write-a-query-to-insert-an-array-with-vary-size-each-time-into-mysql/#findComment-724467 Share on other sites More sharing options...
phppaper Posted December 27, 2008 Author Share Posted December 27, 2008 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!! Quote Link to comment https://forums.phpfreaks.com/topic/138559-write-a-query-to-insert-an-array-with-vary-size-each-time-into-mysql/#findComment-724469 Share on other sites More sharing options...
premiso Posted December 27, 2008 Share Posted December 27, 2008 Gotcha. <?php $values = implode("', '", $array); mysql_query("INSERT INTO table VALUES (" . $values . ")"); ?> Simple as that. Quote Link to comment https://forums.phpfreaks.com/topic/138559-write-a-query-to-insert-an-array-with-vary-size-each-time-into-mysql/#findComment-724471 Share on other sites More sharing options...
phppaper Posted December 27, 2008 Author Share Posted December 27, 2008 WOW, looks good with the implode() function, thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/138559-write-a-query-to-insert-an-array-with-vary-size-each-time-into-mysql/#findComment-724476 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.