Php

Insert PHP Array into MySQL Table

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 –

  1. Using the Repetitive Insert command on each row
  2. Using the Single Insert command by concatenating all array values into array

Also Checkout other dynamic MySQL query tutorials,

Related Post

Option 1: PHP Insert Array into MySQL Using Repetitive Insert Command

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.

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

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().

Conclusion

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.

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.

  • Great! execpt
    the foreach($records as $row) is pointing to the values instead of to index.
    should be:
    foreach($records as $row =>$value)

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

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

2 months ago

What is the Purpose of php_eol in PHP?

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

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories