Insert PHP Array into MySQL Table

This is beginner tutorial of PHP and MySQL,In This post we will learn how to insert php array into mysql table. This is very common problem when we have multiple rows of data that we want to insert into mysql as row.We can do very easily using php to insert array into mysql.

Following code will help to create PHP function to insert array php data into mysql.For Ex, we have following php array.

Also Checkout other dynamic mysql query tutorials,

I want to insert above PHP array into mysql database table.We have Two option to insert php array into MySQL using PHP.

  1. Using Repetitive Insert command on each row
  2. Using Single Insert command by concatenating all array values into array
  3. Option 1: PHP Insert Array into MySQL Using Repetitive Insert Command

    Here we will iterate on each row of data and execute separate insert command for each row.

    I am assuming you have created table and connection with mysql, $conn is connection object,You need to replace 'programming_lang' with your table name and field.* with your column name.i haven’t taken any id for row,I am assuming you have auto incremented 'id' column in your table.

    Option 2: PHP Insert Array into database table Using Single Insert Command

    In this option, we will parse all rows data and store into php array, next we will implode all row data and prepare insert command to insert all php array into mysql table.

    PHP also provide serialize() function to insert php array as as string into mysql.You can store all php array into mysql table as a string using serialize() and revert back php array using unserialize().


    In This Post we have learn how to insert PHP Array into MySQL database table using php.There are two option to insert php array into mysql table.We can also store php array into mysql table using php serialize() function.