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

6
  • Try to use json_encode Commented Sep 13, 2013 at 6:54
  • plz give example of output . Commented Sep 13, 2013 at 6:55
  • @NitishKumar for example i store my above value in db in the form of string like {10,20,30,40} and then i may have to update/delete 20 from the string or i may have to add new value... Commented Sep 13, 2013 at 6:57
  • use implode(',',$a) see this link once php.net/manual/en/function.implode.php Commented Sep 13, 2013 at 7:00
  • @nickle it won't work in this situation.. Commented Sep 13, 2013 at 7:01

4 Answers 4

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}

Sign up to request clarification or add additional context in comments.

3 Comments

i want to store it in db in single attributes.. and my question is in db how to store?
When you pass the array to json_encode, it will be converted into a string, you can directly store that into the database.
@ØHankyPankyØ, You need to read this thread before downvoting. Performance of json_encode vs serialize. stackoverflow.com/questions/804045/…
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

Comments

-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

1 Comment

What about fixing the essentially flawed approach?
-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.