bradgrafelman wrote:If you copy and paste my second code snippet above, you should get the same result.
If so, it must be a problem with your $array string.
I think that is more likely the case. I have an Access database with a customer table like this:
tblcustomer
customerid salutation firstname lastname
1 Mr Joe Bloggs
2 Mr Vincent Vega
3 Mrs Jane Doe
And a MySQL database with a blank customer table like this:
tblcustomer
customerid title firstname lastname
I want to query all the records from tblcustomer in the Access database and insert them into tblcustomer in the MySQL database. Using 'ON DUPLICATE KEY UPDATE' should make sure that existing rows in the MySQL database are updated and new rows are added should they not exist (customerid being an indexed, primary key in both databases).
Here's the code I have presently:
<?php
/*
Connect to Access database
*/
echo "Connecting to Access database...\n";
$access = odbc_connect('access','','');
if (!$access) {
exit("Connect failed: " . $access);
} else {
echo "Success!\n\n";
}
/*
Query Access database
*/
echo "Retrieving customer data...\n";
$cust_data = '';
$cust_query = "SELECT customerid, salutation, firstname, lastname FROM tblcustomer ORDER BY customerid";
$res = odbc_exec($access,$cust_query);
while($r = odbc_fetch_array($res)){
$cust_data .= "('".$r['customerid']."', '".$r['salutation']."', '".$r['firstname']."', '".$r['lastname']."',) ";
}
$cust_data = substr($cust_data,0,-1);
if (!$cust_data) {
exit("Error retrieving customer data!");
} else {
echo "Success!\n\n";
}
odbc_close($access);
/*
Connect to MySQL database
*/
echo "Connecting to MySQL database...\n";
mysql_connect('127.0.0.1:3306', 'user', 'pass') or
die('Could not connect: ' . mysql_error());
mysql_select_db('mysqldb');
echo "Success!\n\n";
/*
Populate/update MySQL database
Increase max_allowed_packet for >1Mb of data
*/
echo "Updating customer data...\n";
$cust_insert = "INSERT INTO tblcustomer (customerid, title, firstname, lastname) Values $cust_data ON DUPLICATE KEY UPDATE title=VALUES(title), firstname=VALUES(firstname), lastname=VALUES(lastname)";
mysql_query(stripslashes($cust_insert)) or die(mysql_error());
echo "Success!\n\n";
?>
All I'm getting right now is
You have an error in your SQL syntax; check the manual that came with your MySQL server version for the right syntax to use near ') ('2', 'Mr', 'Vincent', 'Vega' at line 1
Before insertion the value of $cust_data (when output to a file) is:
('1', 'Mr', 'Joe', 'Bloggs',) ('2', 'Mr', 'Vincent', 'Vega',) ('3', 'Mrs', 'Jane', 'Doe',)
If it's of any significance this is PHP 5.1.4 with MySQL 5.0.27 on Win 2K3 Server.