2017-08-04 93 views
1

我有一個函數在我的PHP腳本中恢復備份數據。一切都很好,運轉良好,直到幾個月的工作良好後突然停止工作。我使用的是OC 2.2.0,此功能應該可以從oc_product_backup表中恢復產品及其數據。我print_r的每一步,這樣我就看到問題的所在,並意識到,當它到達:php腳本返回true突然停止返回值

return true; 

它永遠不會發生。突然間有什麼可能是錯的,我該如何做這項工作?我從來沒有遇到過這樣的問題。我的功能看起來是這樣的:

function restoreBackup() 
{ 
    global $mysqli; 

    $i    = 0; 
    $getpic   = "SELECT * FROM oc_product_backup LIMIT 0, 100000"; 
    $backup   = $mysqli->query($getpic); 

    $mysqli->autocommit(FALSE); 
    $updateproduct_sql  = "UPDATE oc_product SET image = ?, modified_by = ?, date_modified = ? WHERE product_id= ?"; 
    $updatedescription_sql = "UPDATE oc_product_description SET meta_description = ?, meta_keyword = ?, tag = ?, modified_by = ? WHERE product_id = ? AND language_id = ?"; 

    $stmt = $mysqli->prepare($updateproduct_sql); 
    $stmt->bind_param('siss', $image, $modified_by, $date_modified, $product_id); 
    //print_r ($updateproduct_sql); 
    $stmt2 = $mysqli->prepare($updatedescription_sql); 
    $stmt2->bind_param('sssisi', $meta_description, $meta_keyword, $tag, $modified_by, $product_id, $language_id); 
    //print_r($updatedescription_sql); 

    while($row = $backup->fetch_array(MYSQLI_ASSOC)) 
    { 

     //$name    = removeslashes($row['name']); 
     //$name    = $row['name']; 
     //$description  = removeslashes($row['description']); 
     //$description  = $row['description']; 
     $meta_description = $row['meta_description']; 
     $meta_keyword  = $row['meta_keyword']; 
     $tag    = $row['tag']; 
     $product_id  = $row['product_id']; 
     $modified_by  = $row['modified_by']; 
     $language_id  = $row['language_id']; 
     //if($row['language_id'] == 1) 
     //{ 
     $image   = $row['image']; 
     //$ean   = $row['ean']; 
     //$name   = $row['name']; 
     //$model   = $row['model']; 
     //$status   = $row['status']; 
     $price_sync  = $row['price_sync']; 
     $date_modified = $row['date_modified']; 

     if(!$stmt->execute()) 
      return false; 

     //} 
     if(!$stmt2->execute()) 
      return false; 

     $i++; 
     if(($i % 500) === 0) $mysqli->commit(); 

    } 

    $mysqli->commit(); 
    $backup->close(); //the last line that gets executed 
    return true; //this never happens 
    writeToLog('- Backup restored'); 
} 
+0

也許你的腳本超時,因爲數據庫中的數據更多? (只是一個建議,不要釘死我) –

+0

@SchalkKeun Thanx爲您的答覆 - 我的數據庫在整個時間內並沒有顯着增加,所以我不認爲這會是問題。但是,無論如何,這個建議都是對的。 – Nancy

+0

你會得到一個空白頁面或網絡超時頁面?該腳本是在瀏覽器上執行還是作爲cron作業? –

回答

0

看一下代碼後,好像你準備語句結合的數據是環外,因此從技術上講就不會寫入任何數據。

function restoreBackup() { 
    global $mysqli; 

    $i = 0; 
    $getpic = "SELECT * FROM oc_product_backup LIMIT 0, 100000"; 
    $backup = $mysqli - > query($getpic); 

    $mysqli - > autocommit(FALSE); 
    $updateproduct_sql = "UPDATE oc_product SET image = ?, modified_by = ?, date_modified = ? WHERE product_id= ?"; 
    $updatedescription_sql = "UPDATE oc_product_description SET meta_description = ?, meta_keyword = ?, tag = ?, modified_by = ? WHERE product_id = ? AND language_id = ?"; 


    while ($row = $backup - > fetch_array(MYSQLI_ASSOC)) { 

     $meta_description = $row['meta_description']; 
     $meta_keyword = $row['meta_keyword']; 
     $tag = $row['tag']; 
     $product_id = $row['product_id']; 
     $modified_by = $row['modified_by']; 
     $language_id = $row['language_id']; 
     $image = $row['image']; 
     $price_sync = $row['price_sync']; 
     $date_modified = $row['date_modified']; 

     $stmt = $mysqli - > prepare($updateproduct_sql); 
     $stmt - > bind_param('siss', $image, $modified_by, $date_modified, $product_id); 
     if (!$stmt - > execute()) 
      return false; 

     $stmt2 = $mysqli - > prepare($updatedescription_sql); 
     $stmt2 - > bind_param('sssisi', $meta_description, $meta_keyword, $tag, $modified_by, $product_id, $language_id); 
     if (!$stmt2 - > execute()) 
      return false; 

     $i++; 
     if (($i % 500) === 0) $mysqli - > commit(); 

    } 

    $mysqli - > commit(); 
    $backup - > close(); //the last line that gets executed 
    return true; //this never happens 
    writeToLog('- Backup restored'); 
} 
+0

你的解決方案應該可以工作,但它根本沒有。我不知道發生了什麼,我正在失去理智。 – Nancy

+0

@Nancy,我建議你只要在任何地方拋出'print_r()',以確保所有的數據實際上都是按照它的設想書寫的。 –

+0

Thanx試圖幫助。我做了print_r()的一切,一切看起來都很好,除非它不能正常工作。一旦我找到答案,我會嘗試更多並在此發佈答案。如果您有任何其他建議,請告訴我。 – Nancy