2013-02-25 40 views
0

我繼承了一個沒有做它應該做的事情的應用程序。我已將問題隔離到數據庫未正確連接。程序員寫了這個函數,似乎是假設要評估數據庫是否被附加,如果不是,則調用「attachPaymentDatabase()」函數來附加它。代碼沒有意義 - PHP中的兩個條件嘗試

function attachPaymentDatabaseIfNotDoneAlready() 
{ 
global $db; 
global $hasPaymentDatabaseAttached; 
// Determine if we have attached the payment tables, and if not, add them. 
$hasPaymentDatabaseAttached = false; 
try { 
    // this new way should work the best-- looking for PAY. 
    $alldb = queryall($db, "PRAGMA database_list;"); 
    for ($i = 0; $i < count($alldb); $i++) 
     { 
     $alldb[$i] = array_change_key_case($alldb[$i], CASE_LOWER); 
     if (strtolower($alldb[$i]['name']) == 'pay') 
      { 
      debugEmail("condition 1 worked."); 
      $hasPaymentDatabaseAttached = true; 
      break; 
      } 
     } 
    // if its name changed this will also work 
    if (!$hasPaymentDatabaseAttached) 
     { 
     $r = @$db->querySingle("SELECT * FROM PAY_PARAMETER;"); 

     $hasPaymentDatabaseAttached = true; 
     debugEmail("condition 2 worked."); 
     } 
    } 
catch(Exception $e) 
    { 
    } 
if (!$hasPaymentDatabaseAttached) 
    { 
    debugEmail("nothing worked."); 
    attachPaymentDatabase(); 
    } 
} 

我寫了電子郵件我一個定義的消息與時間戳如上使用的debugEmail()函數。當從應用程序執行代碼時,我可以看到「條件2有效」。在「沒有任何工作」之前被稱爲一秒鐘。

我不明白這是怎麼回事。如果debugEmail(「condition 2 working。」);正在執行,那麼應該也是$ hasPaymentDatabaseAttached = true;在這種情況下,這不應該執行:

if (!$hasPaymentDatabaseAttached) 
    { 
    debugEmail("nothing worked."); 
    attachPaymentDatabase(); 
    } 

但它很明顯是。

這是怎麼回事?!?!?!?

+0

調試時必須是你的錯誤(測試錯誤的版本,在錯誤的主機上調試)。 – Kamil 2013-02-25 17:56:05

+0

通過它的聲音,它通過AJAX或其他東西被異步執行兩次,因爲代碼正在做它應該做的事情。刪除那個'@'符號,看看它是否會抑制任何有用的調試信息。除此之外,它看起來像無稽之談。 – ShadowScripter 2013-02-25 17:56:47

+0

好主意@ShadowScripter。也許他應該在try-catch之前添加一些隨機變量'$ function_call_id = mt_rand();'並在電子郵件中發送該變量值。 – Kamil 2013-02-25 18:00:55

回答

0

不,它不應該,因爲$hasPaymentDatabaseAttached在第一個條件中設置爲true。儘管如此,但仍然如上所述。