How to Store A Array In MySQL Database Using PHP

By default mysql doesn't support arrays,Using a simple trick we can store arrays in mysql databases.

Solution 1 :

$data = array('element 1','element 2','element 3','element 4');
$insdata = mysql_escape_string(serialize($data));

Now you can store $insdata as a string in mysql database,When you retrieve data you can use the following code to convert the string to array.

$data = unserialize($row['column']);

Solution 2 :

$data =  array('element 1','element 2','element 3','element 4');
$insdata = implode(",", $data);
$data = explode(",", $row['column']);

No comments:

Post a Comment

Post Your valuable comments here ..........