Sun. May 5th, 2024

sarveshpathak.com

Code Snippets, Tutorials, Articles, Technical Stuff

How to Insert PHP Array into MySQL Database Download free source code.

1 min read
How To Insert PHP Array into MySQL Database using PHP

How To Insert PHP Array into MySQL Database using PHP

In this tutorial, We will learn Following How To Insert Array into MySQL Database PHP and MySQL. MySQL’s databases don’t allow objects or array data types But you can convert any array into a string and save it into the MySQL Table. Here we will see two example to insert an array into a MySQL database. Download free source code.

MySQL TABLE

Create a database called tutorial and create a table called list with fields:

  • Database Name – tutorial
  • Table Name – employees
CREATE TABLE `employees` (
  `id` int(10) NOT NULL,
  `email` varchar(100) NOT NULL,
  `first_name` varchar(100) NOT NULL,
  `last_name` varchar(100) NOT NULL,
  `address` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Example 1) Insert PHP Array into MySQL database using foreach Loop.

<?php
//Insert PHP Array into MySQL Database Download free source code 
$db_conn = mysqli_connect("localhost", "root", "121Dopm", "tutorial");
$user_data  = array(
    "0" => array("[email protected]", "Tom", "Harry","USA"),
    "1" => array("[email protected]", "Harry", "Potter","UK"),
    "2" => array("[email protected]", "Sam", "Nam","US")
);
if(is_array($user_data)){
    foreach ($user_data as $row) {
        $val1 = mysqli_real_escape_string($db_conn, $row[0]);
        $val2 = mysqli_real_escape_string($db_conn, $row[1]);
        $val3 = mysqli_real_escape_string($db_conn, $row[2]);
        $val4 = mysqli_real_escape_string($db_conn, $row[3]);
        $query ="INSERT INTO employees (email,first_name,last_name,address) VALUES ( '".$val1."','".$val2."','".$val3."' ,'".$val4."' )";	
        mysqli_query($db_conn, $query);
    }
}
?> 

Output: Insert PHP Array into MySQL database using foreach Loop.

Insert PHP Array into MySQL database using foreach Loop.
Insert PHP Array into MySQL database using foreach Loop.

Download Source Code : Insert PHP Array into MySQL database using foreach Loop.

Example 2) Insert PHP Array into MySQL database table using onetime.

<?php 
//Insert PHP Array into MySQL Database Download free source code
$db_conn = mysqli_connect("localhost", "root", "121Dopm", "tutorial");
$user_data  = array(
    "0" => array("[email protected]", "Tom", "Harry","USA"),
    "1" => array("[email protected]", "Harry", "Potter","UK"),
    "2" => array("[email protected]", "Sam", "Nam","US")
);
if(is_array($user_data)){
    $DataArr = array();
    foreach ($user_data as $row) {
        $val1 = mysqli_real_escape_string($db_conn, $row[0]);
        $val2 = mysqli_real_escape_string($db_conn, $row[1]);
        $val3 = mysqli_real_escape_string($db_conn, $row[2]);
        $val4 = mysqli_real_escape_string($db_conn, $row[3]);
        $DataArr[] = "('$val1', '$val2', '$val3','$val4')";        
    }
    $sql = "INSERT INTO employees (email,first_name,last_name,address) VALUES";
    $sql .= implode(',', $DataArr);
    mysqli_query($db_conn, $sql);
}
?> 

Output: Insert PHP Array into MySQL database table using onetime.

Insert PHP Array into MySQL database table using onetime.
Insert PHP Array into MySQL database table using onetime.

Download Source Code : Insert PHP Array into MySQL database table using onetime.

Conclusion

In These tutorials, we have learned How to Insert PHP Array into MySQL database table using onetime. and Insert PHP Array into MySQL database using foreach Loop.

About Post Author

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.