How to store array in MySQL

Today I got a question from a person that How to store array in mysql using php. So now I’m going to share the solution for storing array in mysql as if that person don’t know the solution then there will be many people may be also facing the problem.

We can not store Array in mysql directly.First, we have to convert array into string using php serialize function and then save it in our mysql database.

Connect to MySQL

First of all we need to connect to MySQL to store the data.

Create a file with a name config.php and the below php code to connect to mysql.


$con = mysqli_connect("localhost","mysql_username","mysql_password","database_name");


Convert Array into String

Now we have to convert our array into string and for that we will php serialize function.


$array = array("Name"=>"Shubham","Age"=>"17","website"=>"");

$string_array = serialize($array);

echo $string_array;


It’s output will be : a:3:{s:4:"Name";s:7:"Shubham";s:3:"Age";s:2:"17";s:7:"website";s:25:"";}

Save Array data in MySQL

Now after converting array into string we will save it in out mysql database using mysqli_query function.


include 'config.php';

$array = array("Name"=>"Shubham","Age"=>"17","website"=>"");

$serialize = serialize($array);

mysqli_query($con,"INSERT into `table_name` (`column`) VALUES ('$serialize')");


Retrieving data from MySQL


include 'config.php';

$data = mysqli_query($con,"SELECT column FROM table_name");

   $array= unserialize($result['column']);


In the above code we have used unserialize php function to convert the string into array.

I hope you loved it.

Shubham Kumar

Hey, I am Shubham and i love Blogging, Coding and exploring new things and obviously sharing my experience with you.

You may also like...

4 Responses

  1. Alejandro says:

    hey! thanks for the blog just i have one question to ask, how its saved the array in sql, becuse in my phpmyadmin table not appears the array

  2. shane says:

    Thank you so much! Just what i was looking for. Very helpful 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *