zend framework 2 SQL update and insert affected rows (ZF2)

This is a simple question, but I've looked around and couldn't find the answer. How do I extract the number of affected rows from update/insert SQL queries in ZF2?

The following code is working nicely for me (it does update), but I would like to perform proper error checks:

public function updateSomeField($id, $some_field){
    $data = array(
        'some_field'  => $some_field

    $sql = new Sql($this->dbAdapter);
    $update = $sql->update();
    $update->where(array('id' => $id));
    $statement = $sql->prepareStatementForSqlObject($update);

    // need help with the code below...
    // got this from here:
    // http://stackoverflow.com/questions/11491249/zend-framework-db-update-result
    $result = 0;
    try {
        $result = $statement->execute();        // works fine
    } catch (\Exception $e) {
        die('Error: ' . $e->getMessage());
    if (empty($result)) {                       // not sure if this is applicable??
        die('Zero rows affected');

    return $result;                             // ideally, I'd like to return $numRows

Currently, when successful, $result is an object. I tried to vardump it, but it's not showing up the values.

Any help would be appreciated. Thanks.


Have you tried:

if ( $result->count() === 0 ) {
  die('Zero rows affected');

? As far as I remember, it counts anything countable, affected_rows included.

Here is the running code as an answer (because it can be found easier than in comments)

try {
    $affectedRows = $statement->execute()->getAffectedRows();
} catch (\Exception $e) {
    die('Error: ' . $e->getMessage());
if (empty($affectedRows)) {
    die('Zero rows affected');

return $affectedRows;

Need Your Help

Php header for .wav file download but cannot play in embed

php wav

I have to build the header for a .wav file for it to play in the element.

Overflow Hidden on Video Element in Safari

html css html5 safari

I have a video element that I want to hide inside a div with border-radius: 50%;