Jump to content

how do i safely store an array in mysql ?


jasonc

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

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.