• About
  • Advertise
  • Privacy & Policy
  • Contact
djavupixel.com
  • Home
  • Design
    • Fonts
    • Icons
    • Logo
    • Photography
    • Wallpapers
  • Development
    • CSS
    • JS
    • Web
  • WordPress
  • Contact
No Result
View All Result
  • Home
  • Design
    • Fonts
    • Icons
    • Logo
    • Photography
    • Wallpapers
  • Development
    • CSS
    • JS
    • Web
  • WordPress
  • Contact
No Result
View All Result
djavupixel.com
No Result
View All Result
Home Development

Insert multiple rows via a PHP array into MYSQL

djavupixel by djavupixel
May 2, 2015
in Development
1k
0
3.3k
SHARES
10.6k
VIEWS
Share on FacebookShare on Twitter

Sometime you want to pass a large dataset into a mysql table using PHP.

Assembling one INSERT statement with multiple rows is much faster in MySQL than one INSERT statement per row. This sounds like you might be running into string-handling problems in PHP, if fact it is an algorithm problem. Basically, when working with large strings, you want to minimize unnecessary copying. In order to insert hundreds of rows at once, the fastest and most memory efficient way to build a large string is to take advantage of the implode() function and array assignment.

Example of an array with products $product:

Array
(
    [0] => Array
        (
            [ID] => 100
            [NAME] => Apple
            [PRICE] => 0.50
        )

    [1] => Array
        (
            [ID] => 101
            [NAME] => Orange
            [PRICE] => 1.00
        )

    [2] => Array
        (
            [ID] => 102
            [NAME] => Pear
            [PRICE] => 0.60
        )
)

Now the function to insert into MYSQL:

 $sql = array(); 
    foreach( $product as $row ) {
        $sql[] = '('.$row['ID'].', "'.mysql_real_escape_string($row['NAME']).'",
 "'.$row['PRICE'].'")';
    }
    mysql_query('INSERT INTO table (ID, NAME,PRICE) VALUES '.implode(',', $sql));

The advantage of this approach is that you don’t copy and re-copy the SQL statement you’ve so far assembled with each concatenation; instead, PHP does this once in the implode() statement.

Tags: phpTutorial
Previous Post

An Introduction to CSS3 Media Queries

Next Post

Add Dashicons to your WordPress Plugin Admin Menu Icon

Next Post

Add Dashicons to your WordPress Plugin Admin Menu Icon

Leave a Reply Cancel reply

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

Categories

  • CSS
  • Design
  • Development
  • Fonts
  • Icons
  • JS
  • Logo
  • MacOS
  • Photography
  • Uncategorized
  • Wallpapers
  • Web
  • Wordpress

Tags

Air apple car Chrome city Coding communication Corporate Identity Cross Site Scriptings css Design e-Commerce Earth Extensions Free Google Gradient Styles grd Guideline Icons iPhone iPhone4 JavaScript jQuery Landscape Logo mac Nature Navigation osx Photography Photoshop php plugins PSD Sans-Serif security Serif setup SQL Injections Tutorial Water Wordpress WP e-Commerce xss
  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Review
  • Apple
  • Applications
  • Computers
  • Gaming
  • Gear
    • Audio
    • Camera
    • Smartphone
  • Microsoft
  • Photography
  • Security

© 2023 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.