Database Abstraction Layer for PHP

User Tools

Site Tools



This function does no row-locking, so it is possible that the same record could be inserted twice by two programs. Use in multi-user environments is not recommended.

   int replace(
       string $table,
       mixed $dataFields,
       mixed $keyColumn,
       optional bool $autoQuote = false,
       optional bool $primaryKeyIsAuto=false


The function replace() inserts or replace a single record. It can be considered a companion program to autoExecute().



The table name to update


An associative array of data, where the keys are the column names and the values are the values to insert or replace. (you must quote strings yourself).


The primary key field name or if compound key, an array of field names


Set this value to true to use a heuristic to quote strings. Works with nulls and numbers but does not work with dates nor SQL functions.


If the primary key is an auto-inc field, skip in insert.

Return Values

returns 0 = fail, 1 = update, 2 = insert


 * Connection assumed
$tableName = 'products';
$indexColumn = 'product_code';
$data = array('product_code'=>10201,
            'product_name' =>"'Nails'",
            'price' => 3.99);
$status = $db->replace($tableName, $data, $indexColumn);
 * Status code tells us if operation succeeded

Technical Overview

This is not the same as MySQL's replace. ADOdb's Replace() uses update-insert semantics, not insert-delete-duplicates of MySQL. Also note that no table locking is done currently, so it is possible that the record be inserted twice by two programs…

Currently blob replace not supported

v5/reference/connection/replace.txt · Last modified: 2024/03/04 02:44 by peterdd