This question already has answers here:
Importing CSV data using PHP/MySQL
(6 answers)
How to insert multiple rows from array using CodeIgniter framework?
(13 answers)
Closed 3 years ago.
I have a json file and i want to insert all data to my database.
First i am trying to get contents from json file and store in array. It takes 20 seconds.
$strJsonFileContents = file_get_contents('C:\books.json');
$array = json_decode($strJsonFileContents);
After that i prepare the insert query
$query = $db->prepare("INSERT INTO product SET
webID = ?,
name = ?,
subtitle = ?,
title = ?,
description = ?,
ISBN = ?
.
.
.
");
I create a foreach loop and execute insert query.
foreach($array->Books as $book){
$insert = $query->execute(array(
$book->p2,
$book->p3,
$book->p4
.
.
.
));
This foreach loops has 300.000 loop. It takes huge time to insert. - I didn't see finish, it made 40,000 additions in about 10 minutes.
What should I do to reduce this time to seconds? It is not a big problem for me to have 20 seconds to read from the file but inserting time it is a huge problem.
One way to reduce this is to create one query using the following syntax:
INSERT INTO `product` (`webID`, `name`, `subtitle`, `title`, `description`, `ISBN`)
VALUES (?,?,?,?,?,?),(?,?,?,?,?,?) .... // and so on
Using the PHP to assemble the VALUES portion of the query as you can have multiple sets of these. Then run the query ONE time.
Regardless of how you do this, it will take some time to prepare the query and then execute it. Inserting that many rows at once is not trivial.
Related
This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
How to include a PHP variable inside a MySQL statement
(5 answers)
Closed 3 years ago.
I have a MYSQL database query nested in a foreach statement like this:
foreach ($_POST['articles'] as $article => $number) {
$sql_update2 = "INSERT INTO testmerrill.Device_Article_Connection (device_id, article_id)
VALUES ((MAX (device_id) FROM testmerrill.Devices),'{$_POST['articles'][$article]}))'";
query_db1($sql_update2);
}
The above query runs after this one:
$sql_update1 = "INSERT INTO testmerrill.Devices (device_name, device_manufacturer)
VALUES ('{$device_name}', '{$device_manufacturer}')";
query_db1($sql_update1);
This page is the create page of a CRUD program I am writing. I have two tables I am working with here: Devices and Device_Article_Connection. What I am trying to do is insert into my Device_Article_Connection table multiple select statement options the user posted on submit, calling a query for as many options the user chose to include as associated with a new 'device'. I know my current solution is probably not a very elegent way to do it, but thats where I'm at at the moment. The difficulty is that I do not know the id of the new device the user just created, which I need in order to associate it with the articles the user chose to associate with it. I am trying to find the device id using the MAX function (because the last id the user just added should be the largest), but I can't seem to get that to work, there is some syntax error that I have not been able to pinpoint.
I am thankful for any suggestions.
If you use mysqli for a database connection, you could use insert_id to solve this problem:
$conn = new mysqli($server, $user, $pass, $db);
$q = $conn->prepare("INSERT INTO testmerrill.Devices (device_name, device_manufacturer) VALUES (?, ?)"); // Prepare the query
$q->bind_param("ss", $device_name, $device_manufacturer); // Bind variables to prevent SQL injection
$q->execute();
$last_id = $q->insert_id; //Represents the last inserted id
This question already has answers here:
What is the best way to insert multiple rows in PHP PDO MYSQL?
(4 answers)
Insert multiple rows with PDO prepared statements
(4 answers)
PDO MySQL: Insert multiple rows in one query
(1 answer)
Multiple inserts with PDO [duplicate]
(1 answer)
PDO multi insert statement
(1 answer)
Closed 5 years ago.
Sorry if this seems like a simple question but I've searched high and low on Google and Stackoverflow and while there are answers to similar questions, but nothing exactly like what my current situation is. In addition, I just recently returned to programming and first time in ever started using PDO and prepared statements so I've not mastered the art yet.
I have the following code, generating the SQL: (I will have the same code to generate the UPDATE versions to later update these insertions)
if(isset($vars['benefits']))
{
foreach($vars['benefits'] as $benefit)
{
$sql['benefits'][] = "INSERT INTO " . BENEFITS_TABLE . " (benefit) VALUES ('{$benefit['benefit']}')";
}
}
if(isset($vars['sliteratures']))
{
foreach($selectedIDs as $litID)
{
$sql['literature'][] = "INSERT INTO " . PRODLIT_TABLE . " (productID, literatureID) VALUES ('{$productID}', '{$litID}')";
}
}
Which obviously creates the dynamic amount of the query... and now I'm trying to convert it so I could prepare/bind the values (The columns is a hard-coded array while the values are retrieved VIA POST from an HTML form).
I don't know what is the best way to come by doing this. Or how to do it properly might be a better way to phrase that.
I could do that in a loop for each query separately. Like so:
if(isset($vars['benefits']))
{
foreach($vars['benefits'] as $benefit)
{
$sql = "INSERT INTO " . BENEFITS_TABLE . " (benefit) VALUES (:benefit)";
$stmt = $db->prepare($sql);
$stmt->bindParam(":benefit", $benefit['benefit'], PDO::PARAM_STR);
$stmt->execute();
}
}
And the other query likewise, but that puts the SQL operations in a loop. Is that a bad approach?
There would never be a drastic amount of INSERTS. The most would be like 10 for benefits and 3 or 4 for literature so I imagine doing it all in a loop wouldn't effect me much in terms of performance, but for future reference, what would be the best way to come by this effectively?
Thanks for any constructive input.
This question already has answers here:
Insert multiple rows with PDO prepared statements
(4 answers)
Closed 8 years ago.
Ok so i need to insert multiple rows into a database using MySql from one form, each row is a result. The was i started doing it is the following:
INSERT INTO results (1,2,3,4,5,6,7) VALUES (1,2,3,4,5,6,7), (8,9,10,11,12,13,14)
And so on, my question is:
I don't know how many rows of results a user will want to insert ( a maximum of 15 will apply). Is their a more practical way of achieving the same result? Or am I better off sticking to the way i originally planned?
1°) Use prepared statement (like in Java):
(For safe code, always use prepared statement and avoid generate SQL)
$stmt = $db->stmt_init();
$stmt->prepare("INSERT INTO foo_table (id, firstname, lastname) VALUES(?, ?, ?)");
foreach($myarray as $row)
{
$stmt->bind_param('iss', $row['id'], $row['firstname'], $row['lastname']);
$stmt->execute();
}
$stmt->close();
To understant "iss" param in bind_param: http://uk3.php.net/manual/fr/mysqli-stmt.bind-param.php
2°) Generate batch insert:
Batch inserts with PHP
This question already has answers here:
PHP/MySQL insert row then get 'id'
(10 answers)
Closed 9 years ago.
I'm running a MySQL INSERT query where I insert some data into the database.
Each row has a unique ID generated each time a new entry is added. I'd like to get this ID straight after I insert the data.
PHP Code:
$addGiveawayToDb = mysql_query("INSERT INTO $table(orientation, title, color)
VALUES ('$orientation', '$title', '$color')")
or die(mysql_error());
//Here I need to get the row id of the above data...
Thanks!
Try like
$addGiveawayToDb = mysql_query("INSERT INTO $table(orientation, title, color)
VALUES ('$orientation', '$title', '$color')")
or die(mysql_error());
echo "Inserted Id is ".mysql_insert_id();
Makesure that you have an auto increment field in your table.And also try to avoid mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.
See this LINK
This question already has answers here:
Retrieving the last inserted ids for multiple rows
(2 answers)
Closed 9 years ago.
MySQL code is something like:
INSERT INTO table(name, value) VALUES ('name1', 'value1'), ('name2', 'value2'), ('name3', 'value3')
Basically, multiple inserts in the same SQL statement.
How can I get the IDs of the inserted values. I assume mysql_insert_id() combined with the number of inserts isn't safe since someone else might insert something at the same time.
Is there another way?
You either need to insert them one at a time or figure out the id's with a followup query.
INSERT INTO table(primkey, value) VALUES ('pk1', 'val1'), ('pk2', 'val2');
SELECT FROM table id, primkey where primkey in ('pk1', 'pk2');