garethdn
garethdn

Reputation: 12351

How should I store array values in a MySQL database?

To give some background, I'm creating a web app using mostly jQuery. I scrape a number of values from a page on another domain; 3 arrays of integers and a date in string format. These values are then displayed on my page. Each of these arrays has 7 numbers.

It makes sense to me to store each array as a single entry in my MySQL database. My original idea was to store the values of each array as a JSON object and insert that into my entry in the DB from reading around SO this doesn't seem like the approach to take. I don't envisage having any need to sort the values at any stage in the future. I will just need the value of each integer and don't need to know how each integer in each array relates to one another.

Can someone point me in the right direction regarding the storing and retrieval of these values?

Upvotes: 0

Views: 2943

Answers (2)

toxicate20
toxicate20

Reputation: 5410

Usually arrays are kind of iffy to handle. In most cases arrays are values that I need to process later in the front end for the user with javascript (can't think of another use anyway). Therefore what I usually do is store them as text by using

$array = array('one', 'two', 'three');
$textToSaveinDB = implode(',', $array);

When I retrieve this you would just use the other way around

$textFromDB = "one,two,three";
$array = explode(textFromDB, ',')

Afterwards you can do whatever you need to do with the array. For example use JSON_encode() and send it to the user via ajax.

Upvotes: 2

Akhilesh B Chandran
Akhilesh B Chandran

Reputation: 6608

Maybe serialize() and unserialize() would help you. But I won't suggest this as the best approach.

Upvotes: 1

Related Questions