Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

mysql - Number of affected rows Zend DB (UPDATE)

I'm new to Zend Framework and I'd like to know how I can get the number of affected rows from this:

$sql = "UPDATE auth SET act='', status='1' WHERE username = ? AND act = ? ";
$stmt = $this->dbh->prepare($sql);
$stmt->execute(array($this->username, $this->key));

I saw a few posts on this forum, but they we based on MySQLi and SELECT statements where you can actually count the rows using count().

Can anyone suggest how I can alter this to support rowCount.

This is how I connect to my database:

$parameters = array(
    'host' => 'localhost',
    'username' => 'root',
    'password' => '',
    'dbname' => 'users'
);

try {
        $db = Zend_Db::factory('Pdo_Mysql', $parameters);
...

This is in my Bootstrap.php. I did it this way because I work with more than one databases.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Zend_Db_Statement_Pdo has a rowCount() method.

See the API docs

Returns the number of rows affected by the execution of the last INSERT, DELETE, or UPDATE statement executed by this statement object.

This means you can simply:-

$rowsAffected = $stmt->rowCount();

Straight after calling execute() and you should get the number of rows affected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...