2012-12-05 42 views
0

我有一個腳本,循環槽2000客戶端記錄並將它們複製到其他數據庫中。有時它運行的很好,有時它會返回一個mysql錯誤消息,與PHP中發送的查詢不一致。以下是錯誤消息(PA K'2012-12-04 11時05分09' 秒 ) öDU LIC TE EY UPDA):隨機mysql錯誤,但mysql顯示不同的查詢,然後發送

 
    [0] => Array 
     (`enter code here` 
     [Level] => Error 
     [Code] => 1064 
     [Message] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'P A K'2012-12-04 11:05:09' 
     ) 
      O DU LIC TE EY UPDA' at line 45 
     ) 

查詢在PHP印刷當誤差occures:

 
    INSERT INTO 
       `clients` 
      (
       `klantId`, 
       `naam`, 
       `adres`, 
       `postcode`, 
       `plaats`, 
       `telefoon`, 
       `mobiel`, 
       `fax`, 
       `email`, 
       `website`, 
       `post_adres`, 
       `post_postcode`, 
       `post_plaats`, 
       `kvknummer`, 
       `btwnummer`, 
       `rekeningnr`, 
       `postbanknr`, 
       `mailing`, 
       `created`, 
       `modified` 
      )VALUES 
      (
       1134, 
       'Naam', 
       'Adres 9L', 
       '1234 SG', 
       'Plaats', 
       NULL, 
       NULL, 
       NULL, 
       NULL, 
       'www.test.nl', 
       NULL, 
       NULL, 
       NULL, 
       NULL, 
       NULL, 
       NULL, 
       NULL, 
       1, 
       '2012-12-04 11:05:09', 
       '2012-12-04 11:05:09' 
      ) 
      ON DUPLICATE KEY UPDATE 
       `naam`=VALUES(`naam`), 
       `adres`=VALUES(`adres`), 
       `postcode`=VALUES(`postcode`), 
       `plaats`=VALUES(`plaats`), 
       `telefoon`=VALUES(`telefoon`), 
       `mobiel`=VALUES(`mobiel`), 
       `fax`=VALUES(`fax`), 
       `email`=VALUES(`email`), 
       `website`=VALUES(`website`), 
       `post_adres`=VALUES(`post_adres`), 
       `post_postcode`=VALUES(`post_postcode`), 
       `post_plaats`=VALUES(`post_plaats`), 
       `kvknummer`=VALUES(`kvknummer`), 
       `btwnummer`=VALUES(`btwnummer`), 
       `rekeningnr`=VALUES(`rekeningnr`), 
       `postbanknr`=VALUES(`postbanknr`), 
       `mailing`=VALUES(`mailing`), 
       `created`=VALUES(`created`), 
       `modified`=VALUES(`modified`) 
       ; 

我的腳本在公司運行實習,數據庫在數據中心運行extern。腳本運行50%正確50%錯誤...

在此先感謝!

添加腳本循環:

` 
    $dbcon = getDbConnection(); 
    $aClients = getClients(); // return objects 
    foreach ($aClients AS $oClient) { 
    $sQuery = ' INSERT INTO 
     `clients` 
    (
     `klantId`, 
     `naam`, 
     `adres`, 
     `postcode`, 
     `plaats`, 
     `telefoon`, 
     `mobiel`, 
     `fax`, 
     `email`, 
     `website`, 
     `post_adres`, 
     `post_postcode`, 
     `post_plaats`, 
     `kvknummer`, 
     `btwnummer`, 
     `rekeningnr`, 
     `postbanknr`, 
     `mailing`, 
     `created`, 
     `modified` 
    )VALUES 
    (
     ' . $dbcon->real_escape_string($oClient->id) . ', 
     ' . $dbcon->real_escape_string($oClient->naam) . ', 
     ' . $dbcon->real_escape_string($oClient->adres) . ', 
     ' . $dbcon->real_escape_string($oClient->postcode) . ', 
     ' . $dbcon->real_escape_string($oClient->plaats) . ', 
     ' . $dbcon->real_escape_string($oClient->telefoonnummer) . ', 
     ' . $dbcon->real_escape_string($oClient->mobielnummer) . ', 
     ' . $dbcon->real_escape_string($oClient->faxnummer) . ', 
     ' . $dbcon->real_escape_string($oClient->emailadres) . ', 
     ' . $dbcon->real_escape_string($oClient->website) . ', 
     ' . $dbcon->real_escape_string($oClient->afwijkend_adres) . ', 
     ' . $dbcon->real_escape_string($oClient->afwijkend_postcode) . ', 
     ' . $dbcon->real_escape_string($oClient->afwijkend_plaats) . ', 
     ' . $dbcon->real_escape_string($oClient->kvknummer) . ', 
     ' . $dbcon->real_escape_string($oClient->btwnummer) . ', 
     ' . $dbcon->real_escape_string($oClient->bankrekening) . ', 
     ' . $dbcon->real_escape_string($oClient->postbank) . ', 
     ' . $dbcon->real_escape_string($oClient->mailing == 'ja' ? 1 : 0) . ', 
     ' . $dbcon->real_escape_string($oClient->created) . ', 
     ' . $dbcon->real_escape_string($oClient->modified) . ' 
    ) 
    ON DUPLICATE KEY UPDATE 
     `naam`=VALUES(`naam`), 
     `adres`=VALUES(`adres`), 
     `postcode`=VALUES(`postcode`), 
     `plaats`=VALUES(`plaats`), 
     `telefoon`=VALUES(`telefoon`), 
     `mobiel`=VALUES(`mobiel`), 
     `fax`=VALUES(`fax`), 
     `email`=VALUES(`email`), 
     `website`=VALUES(`website`), 
     `post_adres`=VALUES(`post_adres`), 
     `post_postcode`=VALUES(`post_postcode`), 
     `post_plaats`=VALUES(`post_plaats`), 
     `kvknummer`=VALUES(`kvknummer`), 
     `btwnummer`=VALUES(`btwnummer`), 
     `rekeningnr`=VALUES(`rekeningnr`), 
     `postbanknr`=VALUES(`postbanknr`), 
     `mailing`=VALUES(`mailing`), 
     `created`=VALUES(`created`), 
     `modified`=VALUES(`modified`) 
    ;'; 
    $dbcon->query($sQuery); 
}` 

最後編輯: 嘗試在同一臺服務器作爲數據庫服務器上的腳本,然後它不會發生。它發生在某處......

設置是:腳本在服務器A上運行,而數據庫在服務器B上運行。不是相同的網絡,因此互聯網用於數據庫連接。某處的數據丟失了......

+0

你的錯誤信息是否真的丟失了隨機字母?你是否在某個時候重新提交查詢? –

+1

可能有一些數據包含撇號 - 主要發生在名稱或街道名稱內。在這種情況下,查詢沒有正確結束。 – shadyyx

+0

這是確切的輸出。什麼都沒發生。只需將客戶端插入到foreach循環中即可。 – Ajb

回答

0

你不會很好地輸入輸入。如果在PHP中,你必須使所有的值有效mysql_real_escape_string()

'P A K'2012-12-04 11:05:09'看起來你有一個'很多,這會導致錯誤。通過'

逃脫這個Querys的工作,或者他們沒有。沒有50-50改變與好編碼;所以如果查詢起作用,則有一些輸入存在問題。

+0

這就是它,查詢打印出$ sQuery變量。沒有單引號,所有字段都在PHP中轉義。查詢看起來不錯,但不知何故它以某種方式變形。而關於50-50的變化,我也認爲... – Ajb

+0

然後問題可能在循環中。你可以發佈腳本進行循環嗎? – ArendE

+0

我在問題文章中添加了腳本 – Ajb