PHPEveryday.com PHP and Web Development Tutorial
What are you looking for?


Table of Content
PHP ADOdb Tutorial

ADOdb Intro
ADOdb Connection
ADOdb Select
ADOdb show tables
ADOdb show fields
ADOdb show db
ADOdb Caching
ADOdb Recordset
ADOdb Multi DB
ADOdb Dictionary
ADOdb Export
ADOdb Insert
ADOdb Replace
ADOdb Log
ADOdb Pivot Table

ADOdb: Replace Data


Try to update a record, and if the record is not found, an insert statement is generated and executed. Returns 0 on failure, 1 if update statement worked, 2 if no record was found and the insert was executed successfully. This differs from MySQL's replace which deletes the record and inserts a new record. This also means you cannot update the primary key. The only exception to this is Interbase and its derivitives, which uses delete and insert because of some Interbase API limitations.

Formula:

Replace($table, $arrFields, $keyCols,$autoQuote=false)

The parameters are $table which is the table name, the $keyCols which is an associative array where the keys are the field names, and keyCols is the name of the primary key, or an array of field names if it is a compound key. If $autoQuote is set to true, then Replace() will quote all values that are non-numeric; auto-quoting will not quote nulls. Note that auto-quoting will not work if you use SQL functions or operators.

 # single field primary key 
    $ret = $db->Replace('atable', 
    array('id'=>1000,'firstname'=>'Harun','lastname'=>'Al-Rashid'), 
        'id', 
        'firstname',$autoquote = true); 
    # generates UPDATE table SET 
    # firstname='Harun',lastname='Al-Rashid' 
    # WHERE id=1000 
    # or INSERT INTO atable (id,firstname,lastname) 
    # VALUES (1000,'Harun','Al-Rashid') 

    # compound key 
    $ret = $db->Replace('atable2', 
    array('firstname'=>'Harun','lastname'=>'Al-Rashid', 'age' => 33, 'birthday' => 'null'), 
    array('lastname','firstname'), 
        'firstname',$autoquote = true); 

    # no auto-quoting 
    $ret = $db->Replace('atable2', 
    array('firstname'=>"'Harun'",'lastname'=>"'Al-Rashid'", 'age' => 'null'), 
    array('lastname','firstname'), 
        'firstname');



Series this article:
ADOdb: Introduction
ADOdb: Connection Statement
ADOdb: Advance Select Statement
ADOdb: How to show tables
ADOdb: How to show fields
ADOdb: How to show databases
ADOdb: Caching of Recordset
ADOdb: Recordset to HTML
ADOdb: Multi Database Connection
ADOdb: Data Dictionary
ADOdb: Quick Export Data
ADOdb: Insert data style
ADOdb: Replace Data
ADOdb: Log Query
PHP ADOdb: Understanding Pivot Table For Reporting
PHP ADOdb: Creating Pivot Table
PHP ADOdb: Creating Query to Build Pivot Table


Tag: adodb, database, database layer Category: PHP Database Post : October 31st 2007 Read: 4,803 Bookmark and Share

blog comments powered by Disqus


Database Tutorial
  • Learn PHP MySQL
  • Learn PHP ADOdb
  • Learn PHP Data Object/PDO
  • Learn PHP XML
  • Learn PHP SimpleXML
Security Tutorial
  • Learn PHP Security
  • Learn HTTP Authentication
  • Learn PHPSecureSite
Framework Tutorial
  • Learn CodeIgniter
  • Learn Joomla
  • Learn Smarty
  • Learn Zend Framework
Template Tutorial
  • Learn Joomla Template
  • Learn WordPress Template
API Tutorial
  • Learn Facebook
JS Framework Tutorial
  • Learn MooTools
  • Learn JQuery
AJAX Tutorial
  • Learn AJAX in 10 Minutes
  • Learn AJAX Client Side
  • Learn AJAX PHP
  • Learn AJAX Remote Server
  • Learn AJAX Repetitive
  • Learn AJAX MySQL
  • Learn AJAX Grid
Web Services Tutorial
  • Learn Web Services NuSOAP
  • Learn Web Services WSDL
  • Learn Web Services WSDL Array
  • Learn Web Services .NET Grid
  • Learn Web Services WDDX
Package Post
  • Joomla Intro
  • Joomla Component
  • Joomla Module
  • Joomla MVC
  • Joomla MVC Backend
  • PostNuke Intro
  • Zend Framework Intro
  • Zend Framework Action
  • Zend Framework Database
  • Zend Framework Registry
  • Zend Framework Config
  • Zend Framework Login
  • Zend Framework Session
  • PHP Array Tips
  • PHP File Tips
  • PHP Email
  • PHP Ms Excel
  • PHP Pattern
  • PHP SMS
  • Flash Database
  • PHP Multitier
  • jQuery Introduction
  • jQuery Selectors
  • Portable Web Server
  • Web Mobile Intro
  • Drupal Installation
  • Drupal Configuration