简体   繁体   English

当前删除记录数

[英]Present count of deleted records

I have some simple code that deletes every record in a table smaller than a certain date. 我有一些简单的代码,可以删除小于特定日期的表中的每条记录。 I am using PDO as db access method. 我正在使用PDO作为数据库访问方法。

How can I see (and hence report to the user) how many records that were actually deleted in the database? 我如何查看 (从而向用户报告) 数据库中实际删除了多少条记录?

I was thinking transaction (count records to be deleted --> execute delete --> if everything was OK - present counted records to user), but there has to be an easier way, no? 我当时在考虑事务处理(计数记录要删除->执行删除->如果一切正常-将计数的记录提交给用户),但是必须有一种更简单的方法,不是吗?

It seems that execute only returns a boolean value that will only give me an indication of success. 似乎execute只返回一个布尔值,只会给我成功的指示。

$date = new DateTime('2014-06-22 12:00:00');

try{
    $datestring = $date->format('Y-m-d H:i:s');
    $dbh = getConnected($host,$user,$pass,$db);
    $stmt = $dbh->prepare("DELETE FROM sometable WHERE date_and_time < '$datestring'");
    $stmt->execute();
    echo "$rd records deleted"; // where $rd = number of records deleted returned from the query
}
catch(PDOException $e)
{
    echo 'Something went wrong!';
    error_log($e->getMessage().PHP_EOL, 3, "errors.log");
}

$dbh -> connection = null;
$stmt->rowCount();

will give you number of rows affected 将为您提供受影响的行数

PDO rowcount PDO行数

尝试受影响的行功能

printf("rows inserted: %d\n", $stmt->affected_rows);  

If that is mysqli being used (its not stated or clear), in your case, do $stmt->affected_rows 如果正在使用mysqli(未声明或清除),请执行$ stmt-> affected_rows

See http://php.net/manual/en/mysqli-stmt.affected-rows.php http://php.net/manual/en/mysqli-stmt.affected-rows.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM