When you're looking at a speed, I guess it would be better to use LOAD DATA INFILE ( http://dev.mysql.com/doc/m
But again, when it is a small table, you'd probably don't want to bother.
Main Topics
Browse All TopicsHi Everyone.
New and interesting problem. I am extracting data from very obscure db thru odbc and inserting the information into MYsql to be viewd on intanet.
what would be the fastest form of inserting the data into the mysql table. I have created my own " odbc_fetch_assoc() " function to return an array similar to MYsql.
Array ( [odbc_affected_rows] => 0 [ItemID] => 1 [IsInactive] => N [ItemName] => Polyclar QS [ItemNumber] => 17106 [QuantityOnHand] => 29.522000 [ValueOnHand] => 929.940000 [SellOnOrder] => 0.000000 [PurchaseOnOrder] => 0.000000 [ItemIsSold] => Y [ItemIsBought] => Y [ItemIsInventoried] => Y [IncomeAccountID] => 63 [ExpenseAccountID] => 30 [InventoryAccountID] => 138 [Picture] => [ItemDescription] => [UseDescription] => N [CustomList1ID] => 0 [CustomList2ID] => 0 [CustomList3ID] => 0 [CustomField1] => [CustomField2] => [CustomField3] => [BaseSellingPrice] => 40.000000 [PriceIsInclusive] => N [SellTaxCodeID] => 12 [SalesTaxCalcBasisID] => ASP [SellUnitMeasure] => Kg [SellUnitQuantity] => 1 [TaxInclusiveLastPurchaseP
So now i need to extract the values and do an insert.
questions:
1) should i create and totalised array that contains a key for each entry then form a multiple MYsql insert(not really certain how to do that one)
or
2)Should do an insert for each array extracted
I am thinking that maybe the 1st option may be the better option as it is performing only one insert. Is one really big insert better than doing a lot of little inserts?
I hope this is explained clearly to understand
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
When you're looking at a speed, I guess it would be better to use LOAD DATA INFILE ( http://dev.mysql.com/doc/m
But again, when it is a small table, you'd probably don't want to bother.
the issue I have is that the table it is coming from contains between 3500 and 10500 lines and around 40 colums.
I have tried a few querys and get time out errors and errors Like "MYsql has gone"
I am very interested in the file opption Marcel has described. Could you possibly offer some more info as to how i create a file from the array then upload the file into MYsql
To create a text file with tab-separated values that is enquoted:
$handle = @fopen('\var\tmp\insertfil
foreach ($arr as $arrline) {
$txt = "\"" . implode("\"\t\"", $arrline) . "\"\n";
fwrite($handle, $txt);
}
fclose($handle);
You then need the following SQL statement to insert the file:
LOAD DATA LOCAL INFILE '\var\tmp\insertfile.txt' REPLACE
INTO TABLE tbl_name
FIELDS TERMINATED BY '\t' ENCLOSED BY '"' ESCAPED BY '\\'
LINES TERMINATED BY '\n'
where tbl_name is the name of the table (duh). You can use IGNORE instead of REPLACE to ignore the lines that already exist (where REPLACE replaces them).
Also make sure you have the right to read the file (or more important: that mysql has the right to do it). Also make sure that the user using mysql has the right (database)rights to do this.
The code supplied was extremley usefull Thankyou. The only problem is i cannot make this query work
$query = "LOAD DATA LOCAL INFILE '../var/tmp/insertfile.txt
INTO TABLE Items
FIELDS TERMINATED BY '\t'
ENCLOSED BY '\"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n' ";
this the error
check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 6
something to do with single and double quotes i think. Any suggestions
Maybe some more escape characters:
$query = "LOAD DATA LOCAL INFILE '../var/tmp/insertfile.txt
INTO TABLE Items
FIELDS TERMINATED BY '\\t'
ENCLOSED BY '\"'
ESCAPED BY '\\\\'
LINES TERMINATED BY '\\n' ";
I think MySQL expects the characters to be escaped, so you do not want PHP to expand them already.
Business Accounts
Answer for Membership
by: matt_mcswainPosted on 2005-04-10 at 23:06:31ID: 13750636
You can insert everything at once. If the fieldnames are identical in both tables then it won't be much work.
++){ ,';
You can insert into mysql like this:
INSERT INTO tablename (field1, field2, field3) VALUES ('val1', 'val2','val3') , ('val1', 'val2','val3') ,('val1', 'val2','val3') ,etc.
So if you have an array($arr) of associative arrays of all the rows as you have shown above you could do something like:
$sql = 'INSERT INTO tablename (';
$fieldNames = '';
$fieldValues = ' VALUES ';
for($i=0;$i<count($arr);$i
$fieldValues .= '(';
foreach($arr[$i] as $key=>$value)
{
if ($i == 0) $fieldNames .= $key.',';
$fieldValues .= "'$value',";
}
$fieldValues = rtrim($fieldValues,',').')
}
$fieldNames = rtrim($fieldNames,',');
$fieldValues = rtrim($fieldValues,',');
$sql .= $fieldNames .') '.$fieldValues;
mysql_query($sql) or die(msyql_error().$sql);
This is untested, but you can try it or and it there's a sql error it will print it out along with the query.
So try to get the query in the format I post above, or post the query.