簡體   English   中英

代碼沒有意義-嘗試使用PHP中的兩個條件

[英]Code doesn't make sense — two conditionals within PHP try

我繼承了一個應用程序,該應用程序沒有執行應做的事情。 我發現問題出在數據庫沒有正確連接上。 程序員編寫了該函數,該函數似乎是用來評估數據庫是否已附加,如果未附加,則調用“ attachPaymentDatabase()”函數對其進行附加。

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(“條件2正常工作。”); 正在執行,那么$ hasPaymentDatabaseAttached = true也應該如此; 在這種情況下,不應執行以下操作:

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

但這顯然是。

這里發生了什么?!?!?!?

不,不應該這樣,因為$hasPaymentDatabaseAttached在第一個條件下設置為true 仍然毫無意義,但是它可以按照描述的那樣工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM