2014-02-13 106 views
1

我的目標是複製一個本地的MS Access數據庫到我的MySQL數據庫(使用PHP PDO)PHP PDO的MySQL從訪問得到的條目,並插入到MySQL的

的MS Access數據庫位於網絡共享驅動器上,每6小時更新一次新條目。

在下面的代碼中,我從MySQL表'production_schedule'中獲取了最大id號,然後我建立了一個ODBC連接來從MS ACCESS數據庫中檢索大於最大id號的所有條目。

但是現在我無法弄清楚如何將這些新條目插入到MySQL表'production_schedule'中。

任何人都可以請幫忙嗎?

<?php 

/*USING XAMPP*/ 

$dsn = "mysql:host=localhost;dbname=qmsdb;charset=utf8"; 
$uname = "root"; 
$pword = ""; 

$db = null; 
$limit = 10; 
$counter = 0; 

while (true) { 
try { 
    $db = new PDO($dsn, $uname, $pword); 
    $db->exec("SET CHARACTER SET utf8"); 
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 
    $db->setAttribute(PDO::ATTR_PERSISTENT, true); 
    break; 
} 
    catch (Exception $e) { 
     $db = null; 
     $counter++; 
     if ($counter == $limit) 
      throw $e; 
    } 
} 

$aid = $db->prepare("SELECT MAX(id) FROM production_schedule"); 
$aid->execute(); 
$big_id = $aid->fetchColumn(); 
$refid = intval($big_id); 


$conn=odbc_connect('Prod_Schedule','',''); 
if (!$conn) { 
    exit("Connection Failed: " . $conn); 
} 

$sql="SELECT * FROM Schedule WHERE ID > $refid"; 
$rs=odbc_exec($conn,$sql); 
if (!$rs) { 
    exit("Error in SQL"); 
} 

***** INSERT CODE TO PUT THESE MS ACCESS ENTRIES INTO THE MYSQL TABLE ****** 
?> 

回答

2

像這樣也許:

while(odbc_fetch_row($rs)){ 

    $sql = "INSERT INTO production_schedule (fieldName1, fieldName2, fieldName3) VALUES (?, ?, ?)"; 
    $stmt = $dbh->prepare($sql); 

    for($i=1;$i<=odbc_num_fields($rs);$i++){ 
     $stmt->bindValue($i, odbc_result($rs,$i)); 
    } 

    $stmt->execute(); 
} 

注意:取決於你有多少數據必須轉儲,您應該使用像這樣的解決方案:PDO Prepared Inserts multiple rows in single query以降低PHP超時的風險。

+0

使用參數化查詢的+1 –

+0

production_schedule之後的「VALUES」是否屬於該值? – bagofmilk

+0

對不起:/,編輯 – MamaWalter

1

首先創建一個函數,將值插入到MySQL中,然後遍歷ODBC結果;

function createProductionSchedule($company,$person,$order){ 

$mysqli_con=mysqli_connect(DBHOST,DBUSER,DBPASS,DBNAME); 

if (mysqli_connect_errno($mysqli_con)) 
    { 
    echo 'Failed to connect to MySQL'; 
    } 

//Obviously your own fields here 
$company = mysqli_real_escape_string($mysqli_con, $company); 
$person = mysqli_real_escape_string($mysqli_con, $person); 
$order = mysqli_real_escape_string($mysqli_con, $order); 

$sql = "INSERT INTO production_schedule VALUES ('$company','$person','$order')"; 

mysqli_query($mysqli_con, $sql); 

return mysqli_insert_id($mysqli_con); 

mysqli_close($mysqli_con); 
} 

然後在你的代碼段

while (odbc_fetch_row($rs)) 
    { 
    $company=odbc_result($rs,"Company"); 
    $person=odbc_result($rs,"Person"); 
    $order=odbc_result($rs,"Order"); 
    //Call the function to insert the record  
    createProductionSchedule($company,$person,$order); 

    } 


odbc_close($conn); 
+0

你有PDO版嗎? – bagofmilk

+0

+1用於試圖使問題中的數據庫結構與答案匹配。 -1用於「粘合」SQL語句。準備好的陳述將是一個更好的例子。 –

2

我只是測試下面的代碼,它似乎爲我工作好:

$dsn = "mysql:host=localhost;port=3307;dbname=myDb;charset=utf8"; 
$uname = "root"; 
$pword = "whatever"; 
$mysqlDb = new PDO($dsn, $uname, $pword); 
$mysqlDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$mysqlSql = "INSERT INTO clients (LastName, FirstName) VALUES (?, ?)"; 
$mysqlCmd = $mysqlDb->prepare($mysqlSql); 
$LastName = ''; 
$FirstName = ''; 
$mysqlCmd->bindParam(1, $LastName, PDO::PARAM_STR, 255); 
$mysqlCmd->bindParam(2, $FirstName, PDO::PARAM_STR, 255); 

$connStr = 
     'Driver={Microsoft Access Driver (*.mdb, *.accdb)};' . 
     'Dbq=C:\\Users\\Public\\Database1.accdb;'; 
$accessDb = odbc_connect($connStr, "", ""); 
$accessSql = "SELECT LastName, FirstName FROM Clients"; 
$accessResult = odbc_exec($accessDb, $accessSql); 
while ($accessData = odbc_fetch_array($accessResult)) { 
    $LastName = $accessData["LastName"]; 
    $FirstName = $accessData["FirstName"]; 
    $mysqlCmd->execute(); 
}