簡體   English   中英

PDO拒絕刪除一行數據(在特定表中)

[英]PDO refuses to delete a row of data (in specific table)

好的,我很難過這個。 我在我的數據庫中有一個表,我似乎無法通過PDO刪除行(我已經注意到這種行為幾周了,它在此之前完美地工作)。

我的PHP代碼是這樣的:

    // Echo's have been added for testing.
    try{
            $dbh = new PDO($hostname, $username, $password);

            $sql="delete from sources where workFlowID=".$ID.";";
            $numRows=$dbh->exec($sql);
            echo "There were $numRows deleted with: $sql <br><br>";

            $sql="delete from workflow where id=".$ID." limit 1;";
            // I have only put the 'or die' section in this today to try to see where
            // it was having problems. It carries through happily as the output 
            // below shows.
            $numRows=$dbh->exec($sql) or die(print_r($dbh->errorInfo(), true));
            // This is the problem delete...
            echo "There were $numRows deleted with: $sql <br><br>";

            $dbh=null;

    }
    catch(PDOException $e){
            echo 'Error : '.$e->getMessage();
            exit();
    }

隨着輸出:

    There were 1 deleted with: delete from sources where workflowid=1;

    There were 1 deleted with: delete from workflow where id=1 limit 1;

但是,當我查看數據庫時,我仍然看到我的記錄位於我的workflow表中。

我檢查並仔細檢查了數據類型。 ID和workflowID都是整數。 它們被提供相同的變量幾行。

我認為這可能是一個權限問題,所以我已經覆蓋了以下內容:

    mysql> grant all privileges on storeProcess.* to 'myusername'@'localhost' with
     grant option;
    Query OK, 0 rows affected (0.19 sec)

然后我認為它可能是一些時髦的計時/重載/ whothehellknows什么問題,所以我在工作流表上創建了一個觸發器來完成應用程序的工作並清理其他表。 然后我將我的PHP代碼更改為:

    // Echo's have been added for testing.
    try{
            $dbh = new PDO($hostname, $username, $password);

            $sql="delete from workflow where id=".$ID." limit 1;";
            $numRows=$dbh->exec($sql) or die(print_r($dbh->errorInfo(), true));
            echo "There were $numRows deleted with: $sql <br><br>";

            $dbh=null;

    }
    catch(PDOException $e){
            echo 'Error : '.$e->getMessage();
            exit();
    }

現在的輸出是:

    There were 1 deleted with: delete from workflow where id=1 limit 1;

但同樣,記錄仍然存在。

    mysql> select count(*) from workflow where id=1;
    +----------+
    | count(*) |
    +----------+
    |        1 |
    +----------+
    1 row in set (0.00 sec)

當我使用PDO正在使用的帳戶的用戶名/密碼登錄時,我當然沒有問題從控制台刪除具有完全相同命令的記錄(觸發器工作正常並從其余表中刪除數據):

    mysql> delete from workflow where ID=1;
    Query OK, 1 row affected (0.00 sec)

    mysql> select count(*) from workflow where id=1;
    +----------+
    | count(*) |
    +----------+
    |        0 |
    +----------+
    1 row in set (0.00 sec)

出了什么問題?

這是在我的工作桌面上運行的(Win XP,沒有什么花哨的,來自大公司的標准型SOE)。

我在這些查詢期間沒有使用任何交易。 此外,只有少數用戶使用該應用程序,並且在任何情況下都不會達到高CPU。

我將把代碼和架構帶回家在linux下測試它,並在我回來時發布結果。

更新:我剛剛在家里把它移到了我的linux系統。 完全沒有變化。

編輯:

我按照建議更改了我的代碼:

try{
    $dbh = new PDO($hostname, $username, $password);
    $dbh->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql="delete from sources where workflowid=".$ID.";";

    //echo $sql."<br><br>";

    $numRows=$dbh->exec($sql);
    echo "There were $numRows deleted with:<b> $sql </b><br><br>";

    $sql="delete from workflow where id=".$ID." limit 1;";
    $numRows=$dbh->exec($sql);
    echo "There were $numRows deleted with:<b> $sql </b><br><br>";

    $sql="delete from workflow where id=".$ID." limit 1;";
    $numRows=$dbh->exec($sql);
    echo "There were $numRows deleted with:<b> $sql </b><br><br>";

    $dbh=null;

}
catch(PDOException $e){
    echo 'Error : '.$e->getMessage();
    //exit();
}

我使用以下輸出運行它:

There were 601 deleted with: delete from sources where workflowid=77;

There were 1 deleted with: delete from workflow where id=77 limit 1;

There were 0 deleted with: delete from workflow where id=77 limit 1; 

該行仍未刪除:

mysql> select count(*) from workflow where id=77;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
1 row in set (0.00 sec)

PDO::exec()函數返回受影響的行數,如果沒有行受影響,則返回0。

像這樣的行將die()因為exec將返回0 ,這被解釋為布爾值false。

$dblink->exec("UPDATE `sometable` SET `somecolumn`=0 WHERE `somecolumn`=0") or die("Never use die for error handling.");

PDO的最佳錯誤處理實踐是使用PDO異常。 啟用PDO異常(PDOException類,請參閱docs),如下所示:

//enable Exception mode (uncaught exceptions work just like die() with the benefit of giving you details in logs of where execution was stopped and for what reason)
$pdoDBHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

刪除or die()exit(); 並啟用例外模式。 我打賭這會解決你的“怪異”問題。 另外看看在PHP中拋出異常,即使是使用過程代碼(替換die()exit()

BTW exit就像die一樣停止執行,除了它在CLI模式下有用,因為它向操作系統返回成功/錯誤代碼。 它真的不適合錯誤處理。

暫無
暫無

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

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