phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


ADOdb: Replace Data

Tag: adodb, database, database layer   Category: PHP Database
post: 31 Oct 2007 read: 1,911


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

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting