2

i want to store my PHP values in db(mysql) in the form of array...

for example

$a=array{10,20,30,40}; 

i want to store this variable $a in to db in the array form like how it's storing in array using index.

why i want to do this because in future i may have to perform update or delete operation on the array values..

i know that it's possible to do this thing... but i don't know how to implement this..

i searched about this topic but i didn't get proper answer....

Please suggest me how to do this things...

404 Not Found
  • 1,223
  • 2
  • 22
  • 31

4 Answers4

2

Why don't use json_encode in PHP and store it on your database. It's the best way.

The array will be converted to a string and will be stored. Retrieve the data and make use of json_decode and then start working as per your needs.

Example:

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
?>

OUTPUT:

{"a":1,"b":2,"c":3,"d":4,"e":5}

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
2

You should create a distinct TABLE to store this kind of data.
Consists of 2 columns, corresponding record ID and the actual data.

So, your record will be looks like

rid value
1   10
1   20
1   30
1   40
2   10
2   40
...

this way you will be able to perform update or delete operation on the array values using conventional SQL routines, as well as selecting data based on the array values.

This is how the things done oin the real world, not in PHP sandbox.

All othe answers here are plainly wrong

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-1

I would use serialize/unserialize for this. You can use it like this:

Send to MySQL

<?php
    $a = array{10,20,30,40};
    $a = serialize($a);
    // your code here to send it to the mysql
?>

Get from MySQL

<?php
    // your code here to collect it from mysql
    $a = unserialize($mysql->str);
?>

The field in the MySQL should be TEXT or VARCHAR.

Regards

BlackBonjour

BlackBonjour
  • 91
  • 1
  • 1
  • 6
-2

You can always serialize your array then store the result in a VARCHAR or TEXT field and after fetching you can unserialize the field.

fabrik
  • 14,094
  • 8
  • 55
  • 71