public function upsert($table, $id, array $data)
    {
        if (isset($this->prepared[$table]['ref']) && $this->execute($table, $id)) {
            $data[] = $id;
            if ($row = $this->fetch($table)) {
                return ($this->execute($this->prepared[$table]['ref']['update'], $data)) ? array_shift($row) : false;
            } else {
                return $this->execute($this->prepared[$table]['ref']['insert'], $data);
            }
        }
        $first = each($data);
        $single = (is_array($first['value'])) ? $first['value'] : false;
        if ($single) {
            $data = array_keys($single);
        }
        if ($stmt = $this->prepare("SELECT {$id} FROM {$table} WHERE {$id} = ?", 'row')) {
            $this->prepared[$stmt]['ref']['update'] = $this->update($table, $id, $data);
            $this->prepared[$stmt]['ref']['insert'] = $this->insert($table, array_merge($data, array($id)));
        }
        if ($single && $stmt) {
            $id = $this->upsert($stmt, $first['key'], array_values($single));
            $this->close($stmt);

            return $id;
        }

        return $stmt;
    }
public function upsert($table, $id, array $data)
    {
        if (isset($this->prepared[$table]['ref']) && $this->execute($table, $id)) {
            $data[] = $id;
            if ($row = $this->fetch($table)) {
                return ($this->execute($this->prepared[$table]['ref']['update'], $data)) ? array_shift($row) : false;
            } else {
                return $this->execute($this->prepared[$table]['ref']['insert'], $data);
            }
        }
        $first = each($data);
        $single = (is_array($first['value'])) ? $first['value'] : false;
        if ($single) {
            $data = array_keys($single);
        }
        if ($stmt = $this->prepare("SELECT {$id} FROM {$table} WHERE {$id} = ?", 'row')) {
            $this->prepared[$stmt]['ref']['update'] = $this->update($table, $id, $data);
            $this->prepared[$stmt]['ref']['insert'] = $this->insert($table, array_merge($data, array($id)));
        }
        if ($single && $stmt) {
            $id = $this->upsert($stmt, $first['key'], array_values($single));
            $this->close($stmt);

            return $id;
        }

        return $stmt;
    }
public function upsert ( $ table , $ id , array $ data ) { if ( isset ( $ this -> prepared [ $ table ] [ 'ref' ] ) && $ this -> execute ( $ table , $ id ) ) { $ data [ ] = $ id ; if ( $ row = $ this -> fetch ( $ table ) ) { return ( $ this -> execute ( $ this -> prepared [ $ table ] [ 'ref' ] [ 'update' ] , $ data ) ) ? array_shift ( $ row ) : false ; } else { return $ this -> execute ( $ this -> prepared [ $ table ] [ 'ref' ] [ 'insert' ] , $ data ) ; } } $ first = each ( $ data ) ; $ single = ( is_array ( $ first [ 'value' ] ) ) ? $ first [ 'value' ] : false ; if ( $ single ) { $ data = array_keys ( $ single ) ; } if ( $ stmt = $ this -> prepare ( "SELECT {$id} FROM {$table} WHERE {$id} = ?" , 'row' ) ) { $ this -> prepared [ $ stmt ] [ 'ref' ] [ 'update' ] = $ this -> update ( $ table , $ id , $ data ) ; $ this -> prepared [ $ stmt ] [ 'ref' ] [ 'insert' ] = $ this -> insert ( $ table , array_merge ( $ data , array ( $ id ) ) ) ; } if ( $ single && $ stmt ) { $ id = $ this -> upsert ( $ stmt , $ first [ 'key' ] , array_values ( $ single ) ) ; $ this -> close ( $ stmt ) ; return $ id ; } return $ stmt ; }
Either update or insert records depending on whether they already exist or not.

@param string|int $table Either the database table name, or the prepared statement id you got from calling this the first time.  You cannot include *'SET'* or *'INTO'* here.
@param array      $id    Either the name of the column with the unique identifier you will be referencing (when preparing a statement), or the unique identifier of the column you are upserting.
@param array      $data  Either the table column names (when preparing a statement), or a row of values (to upsert).  If you are only upserting one record and want to save yourself some typing, then make this an ``array(name => value, ...)`` of columns.

@return bool|int Either ``false`` if there was an error, a prepared ``$stmt`` to keep passing off as the **$table**, or the ``$id`` of the row that was upserted.  Don't forget to ``$db->close($stmt)``.

```php
if ($stmt = $db->upsert('employees', 'id', array('name', 'title'))) {
$db->upsert($stmt, 101, array('Roberto Cratchit', 'CEO'));
$db->upsert($stmt, 106, array('John Smith', 'Developer'));
$db->close($stmt);
}

$db->upsert('employees', 'id', array(
107 => array(
'name' => 'Ella Minnow Pea',
'title' => 'Executive Assistant',
),
));
```
Either update or insert records depending on whether they already exist or not .