In This post, we will learn how to insert a PHP array into MySQL table. This is a very common problem when we have multiple rows of data that we want to insert into MySQL as a row. We can do this very easily using PHP to insert an array into MySQL.
This is a beginner tutorial on PHP and MySQL, You’ll learn here basic MySQL operations with PHP.
The following code will help to create a PHP function to insert array PHP data into MySQL.For Ex.
We have the following PHP array.
$records = array( "0" => array("Parvez", "PHP", "12"), "1" => array("Devid", "Java", "34"), "2" => array("Ajay", "Nodejs", "22") );
I want to insert the above PHP array into the MySQL database table. We have two options to insert a PHP array into MySQL using PHP.
We will follow the below steps to insert arrays of data into MySQL –
Also Checkout other dynamic MySQL query tutorials,
Here we will iterate on each row of data and execute separate insert commands for each row.
if(is_array($records)){ foreach ($records as $row) { $fieldVal1 = mysqli_real_escape_string($conn, $row[0]); $fieldVal2 = mysqli_real_escape_string($conn, $row[1]); $fieldVal3 = mysqli_real_escape_string($conn, $row[2]); $query ="INSERT INTO programming_lang (field1, field2, field3) VALUES ( '". $fieldVal1."','".$fieldVal2."','".$fieldVal3."' )"; mysqli_query($conn, $query); } }
I am assuming – You have created a 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 a row, I am assuming you have auto-incremented 'id'
column in your table.
In this option, we will parse all row data and store it into a PHP array, Next step is – we will implode all row data and prepare an insert SQL command to insert all PHP array data into the MySQL table.
if(is_array($records)){ $DataArr = array(); foreach($records as $row){ $fieldVal1 = mysqli_real_escape_string($conn, $row[0]); $fieldVal2 = mysqli_real_escape_string($conn, $row[1]); $fieldVal3 = mysqli_real_escape_string($conn, $row[2]); $DataArr[] = "('$fieldVal1', '$fieldVal2', '$fieldVal3')"; } $sql = "INSERT INTO programming_lang (field1, field2, field3) values "; $sql .= implode(',', $DataArr); mysqli_query($conn, $query); }
PHP also provides serialize()
function to insert a PHP array as a 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 learned how to insert PHP Array into the MySQL database table using PHP. There are two options to insert a PHP array into the MySQL table. We can also store PHP array into MySQL table using php serialize()
function.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
One problem with serialized data is that it is not searchable via mysql query.. It is still better to insert each of the data one row each in the database table.
Ya, you right.Its useful when we need to store bunch of data into an object.
Great! execpt
the foreach($records as $row) is pointing to the values instead of to index.
should be:
foreach($records as $row =>$value)
wow, you save me.
something learn from you thank you.