php – PDO拒绝删除一行数据(在特定表中)
好的,我很难过这个.我在我的数据库中有一个表,我似乎无法通过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; 但是,当我查看数据库时,我仍然看到我的记录位于我的工作流程表中. 我检查并仔细检查了数据类型. 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,$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,$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.
像这样的行将死亡()因为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); 删除或死()并退出();并启用例外模式.我打赌这会解决你的“怪异”问题.另外看看在PHP中抛出异常,即使是使用过程代码(替换die()和exit()). BTW出口就像死掉一样停止执行,除了它在CLI模式下有用,因为它向操作系统返回成功/错误代码.它真的不适合错误处理. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |