jasonc Posted July 14, 2011 Share Posted July 14, 2011 I have looked around and finding so many different ways to do this that i would like to know the best and safest way to store my array in a single field in mysql. my array will look something like this... $array['item_id'] = "I1234"; $array['item_name'] = "Items name"; $array['item_tax'] = "12.34"; $array['item_postage'] = "12.34"; What I was hoping to do is store the POSTS I get from PayPal, the problem is when there is more than one item in the cart as the field name changes and what if cater for 10 items and the customer orders 11 or if I cater for 100 and they order 101 !! This seems a very long way around storing the information. How do I get around the change in field names ie. a single item would give item_name item_number and so on but the multiple items would give item_name1 item_number1 item_name2 item_number2 and so on. so I would have to create a table with a stack of fields for many items, enough to cater for all possibilities!! not practical. how can I store all the items in a better way other than storing an array in mysql ? the tnx_id would of course be in its own field to prevent muliple records of the same order for when paypal send further information about the payment say 'pending' then later on 'completed' or 'failed' or what ever it may be. I am wanting to update(overwrite)/store the rest of the info that is in the POSTS. Quote Link to comment https://forums.phpfreaks.com/topic/241947-how-do-i-safely-store-an-array-in-mysql/ Share on other sites More sharing options...
fenway Posted July 15, 2011 Share Posted July 15, 2011 You store multiple rows, not arrays. Quote Link to comment https://forums.phpfreaks.com/topic/241947-how-do-i-safely-store-an-array-in-mysql/#findComment-1243028 Share on other sites More sharing options...
ebmigue Posted July 16, 2011 Share Posted July 16, 2011 I was confused. What is it that you worry about? The security of your data? Or the structure or your data? If security: you can always try the conventional encryption/decryption functions/operators available in PHP. Or write your own. If structure: I suggest you use the relational structure. That is, tables. They are better documented. Of course, deciding on a structure might involve performance considerations. But if using an arbitrary structure only has negligible improvement in performance, then using a better-documented structure is more advantageous. Quote Link to comment https://forums.phpfreaks.com/topic/241947-how-do-i-safely-store-an-array-in-mysql/#findComment-1243365 Share on other sites More sharing options...
TeNDoLLA Posted July 16, 2011 Share Posted July 16, 2011 If you really want to store arrays in database you can use PHP's serialize() function to make it a string and store that into varchar/text field. When fetching data use unserialize on the string to get it back as an array. But don't know about the safety here... Quote Link to comment https://forums.phpfreaks.com/topic/241947-how-do-i-safely-store-an-array-in-mysql/#findComment-1243455 Share on other sites More sharing options...
fenway Posted July 16, 2011 Share Posted July 16, 2011 The only excuse for storing arrays is that (a) you'll never need to examine the individual array elements and (b) you and/or your application is/are lazy. Quote Link to comment https://forums.phpfreaks.com/topic/241947-how-do-i-safely-store-an-array-in-mysql/#findComment-1243487 Share on other sites More sharing options...
jeff5656 Posted July 16, 2011 Share Posted July 16, 2011 The only excuse for storing arrays is that (a) you'll never need to examine the individual array elements and (b) you and/or your application is/are lazy. Actually I'm just the opposite. I'm too lazy to understand how to store and retrieve arrays so I just make another table in the database :-) Quote Link to comment https://forums.phpfreaks.com/topic/241947-how-do-i-safely-store-an-array-in-mysql/#findComment-1243494 Share on other sites More sharing options...
fenway Posted July 17, 2011 Share Posted July 17, 2011 That's preferable. Quote Link to comment https://forums.phpfreaks.com/topic/241947-how-do-i-safely-store-an-array-in-mysql/#findComment-1243836 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.