dscpg80066
2017-06-01 10:59
浏览 33
已采纳

删除记录,如果它存在于php / PDO中[重复]

This question already has an answer here:

I have some code that deletes a record from a MySQL database, but I get the success message even if the record does not exist. I have searched for ways of producing a message if the record does not exist but cannot find one that works with my code.

My code is:

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    /*** echo a message saying we have connected ***/
    //echo 'Connected to database<br />';
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "DELETE FROM ukgh WHERE telephone = :telephone";
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(':telephone', $telephone, PDO::PARAM_STR);   
    $stmt->execute(); 

    /*** close the database connection ***/
    $dbh = null;
}
catch(PDOException $e) {
    echo $e->getMessage();
}
// once processing is complete
// show success message
echo 'Success - The record for ' . $telephone . ' has been deleted.';
?>

Note to Fred-ii- The answer you referred to is correct, but the terminology in the question I asked was different. I asked how to ensure the RECORD I was deleting existed. I did not ask how to check if a row exists, and would never have thought to search for anything about an existing ROW when I was looking for an existing RECORD. To some experts like yourself a row may be the same as a record, but I and possibly other less enlightened people like me have never heard a record called a row. Best wishes, Tog

</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 检查mysql是否存在行 3 answers \ n

    我有一些代码可以从MySQL数据库中删除记录,但即使是 记录不存在。 如果记录不存在但找不到符合我代码的记录,我已经搜索了生成消息的方法。

    我的代码是:

     尝试{
     $ dbh =新PDO(“mysql:host = $ hostname; dbname = $ dbname”,$ username,$ password); 
     / ***回显一条消息,说我们已连接**  * / 
     // echo'Connected to database&lt; br /&gt;'; 
     $ dbh-&gt; setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); 
     $ sql =“DELETE FROM ukgh WHERE telephone =  :telephone“; 
     $ stmt = $ dbh-&gt; prepare($ sql); 
     $ stmt-&gt; bindParam(':telephone',$ telephone,PDO :: PARAM_STR);  
     $ stmt-&gt; execute();  
     
     / ***关闭数据库连接*** / 
     $ dbh = null; 
    } 
    catch(PDOException $ e){
     echo $ e-&gt; getMessage(); 
    } \  n //一旦处理完成
     //显示成功消息
    echo'成功 - '的记录'。  $电话。  '已被删除。'; 
    ?&gt; 
       
     
     

    请注意Fred-ii-您提到的答案是正确的,但问题I中的术语 问是不同的。 我问如何确保我删除的RECORD存在。 我没有问如何检查一行是否存在,并且当我在寻找现有的RECORD时,我从未想过要搜索有关现有ROW的任何内容。 对于像你这样的专家来说,一行可能和唱片一样,但我和其他像我这样不那么开明的人从来没有听过一个叫做行的记录。 祝福,Tog

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dsgk0386 2017-06-01 11:05
    最佳回答

    Use rowCount(); inside try to check number of affected rows by your sql statement

    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
        /*     * * echo a message saying we have connected ** */
    //echo 'Connected to database<br />';
        $sql = "DELETE FROM ukgh WHERE telephone = :telephone";
        $stmt = $dbh->prepare($sql);
        $stmt->bindParam(':telephone', $telephone, PDO::PARAM_STR);
        $stmt->execute();
        $count = $stmt->rowCount();// check affected rows using rowCount
        if ($count > 0) {
            echo 'Success - The record for ' . $telephone . ' has been deleted.';
        } else {
            echo "Your error message";
        }
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
    

    Read http://php.net/manual/en/pdostatement.rowcount.php

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题