Prashant Kumbhar Prashant Kumbhar - 19 days ago 9
MySQL Question

How to Store Array in Mysql Using PHP

I want to store array in mysql database. with key and value

Following are code i used

$name= "prashant";
$lname= "kumbhar";
$test = "testing";

$array = ('fname'=>$name,
'lname'=>$lname,
'test' = $testing
);


Store these $array in database using php

Answer

Use json_encode() to store data in mysql table.

<?php
$name= "prashant";
$lname= "kumbhar";
$test = "testing";

$array = array('fname'=>$name,
               'lname'=>$lname,
               'test' => $test
         );

$res = json_encode($array);
echo "Convert from array to json :" .$res;

echo "\n\nFrom json to array:\n";
print_r(json_decode($res));

output

Convert from array to json :
{"fname":"prashant","lname":"kumbhar","test":"testing"}

From json to array:
stdClass Object
(
    [fname] => prashant
    [lname] => kumbhar
    [test] => testing
)

At the time of retrieve data in array form then use json_decode()

Working Demo : Click Here

Comments