duankao4489 2016-03-05 02:57
浏览 40
已采纳

如果我更改查询中的值,则回显的结果保持不变

I have this class User with a method inside it called deleteFriend. This uses a query using PDO to delete a row in the database table.

$db = new PDO('mysql:host=localhost;dbname=goal;charset=utf8','root','');
$auth_id = 5;
$user_user_id = 6;


echo User::deleteFriend($auth_id, $user_user_id, $db);
echo User::deleteFriend($user_user_id, $auth_id, $db);

As you can see this method is called 2 ways. Delete when auth_id is 5 or 6. And when user_user_id is 6 or 5. Below is the query which has the added parameter that accepted must be 1.

public function deleteFriend($auth_id, $user_id, $db){
    $stmt = $db->prepare("DELETE FROM `friends` WHERE `user_id` = ? AND `friend_id` = ? AND `accepted` = '1'");
    $stmt->bindParam(1, $auth_id);
    $stmt->bindParam(2, $user_id);

    if($stmt->execute()){
        return 1;
    }else{
        return 2;
    }
}

Now heres that part that interests me. Below you can see I have 2 rows. One with a user_id of 6 and another of 5. And the other row has a friend_id of 5 and the other 6.

enter image description here

When these methods are run with these values they both return 1. Even though the method that refers to the row with accepted of 0 does not delete and the one that has accepted of 1 does delete. My guess here is that the $stmt->execute() is returning true for the method that has accepted of 0. But why? It isn't satisfying the criteria for the query because accepted does not = 1. The functions work as they are supposed to and for my use it dosn't matter if it returns 1 or 2 because the end result is the same (it only deletes the correct row with accepted of 1) but I am curious as to what the reason for this is. If anyone can shed some light on this I'd really appreciate it. And apologizes if my explanation of the question is confusing. I am confused myself. :-(

  • 写回答

1条回答 默认 最新

  • douya2433 2016-03-05 03:03
    关注

    PDOStatement::execute() returns true when the query is successful. If you DELETE and no rows match the WHERE clause, then you have successfully deleted zero rows.

    PDOStatement::execute() only returns false if some occurrence interferes with the execution of the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题