- skip to content

# Database Abstraction Layer

### User Tools

- Login

### Site Tools

- Recent changes
- Media Manager
- Sitemap

### Sidebar

##### Resources

- Reference Manual
- User Guide
- The Data Dictionary
- ADOdb-xmlschema
- ADOdb Active Record
- Performance Monitoring
- Date/Time Library
- Session Management Library
- Unsupported Addons
- Coding Standards

##### Supported Databases

- Access
- ADO
- CSV
- Firebird
- Frontbase
- IBM DB2
- Informix
- Interbase
- LDAP
- Microsoft SQL Server
- MySQL
- Netezza
- ODBC
- ODBTP
- Oracle
- PDO
- Postgresql
- SAP ADS
- SAP ASE
- SAP Maxdb
- SAP Sqlanywhere
- Sqlite
- Sybase
- Plain Text
- Visual Foxpro

##### Other

- Feature Comparison Matrix
- Connection Matrix
- Include File Guide

v5:reference:connection:autoexecute

# autoExecute

autoExecute is designed to provide a robust simple interface to record updating and insertion. As such, it contains multiple layers of table, column and data validation to ensure data integrity prior to record insertion or update. Consequently, there may be some performance degradation when used for large numbers of record updates. Consider using execute() for that purpose instead.

##### See Also

$ADODB_QUOTE_FIELDNAMES

##### Syntax

      bool autoExecute(
         string $tables,
         string[] $arrFields,
         string $mode,
         optional mixed $where=false
         optional bool $forceUpdate=false
         optional bool $dontFixQuotes=false
         )

## Description

The function `autoExecute()` can automatically generate and execute INSERT and UPDATE statements on a given table with this function, and is a wrapper for GetInsertSQL() and GetUpdateSQL()

AutoExecute() inserts or updates `$table` given an array of `$arrFields`, where the keys are the field names and the array values are the field values to store. Note that there is some overhead because the table is first queried to extract key information before the SQL is generated. An INSERT or UPDATE is generated based on `$mode` (see below).

### $table

The name of the table to update

### $arrFields

An associative array of field⇒value pairs

### $mode

Legal values for `$mode` are

String | Integer | Constant
--- | --- | ---
INSERT | 1 | DB_AUTOQUERY_INSERT
UPDATE | 2 | DB_AUTOQUERY_UPDATE

### $where

The $where clause is required if $mode == 'UPDATE'.

### $forceUpdate

Value | Description | Overhead
--- | --- | ---
false | (default). Only fields that have been modified are updated | The database record is re-read before update
true | All fields provided are updated | The constructed statement may be considerably larger

### $dontFixQuotes

For a description of this parameter, see qStr()

### Usage

AutoExecute($table, $arrFields, $mode, $where=false, $forceUpdate=true,$magicq=false)

#### Insert example

    $table               = 'people'
    $record["first_name"] = "Carol";
    $record["last_name"]  = "Smith";

    $conn->AutoExecute($table,$record,'INSERT');

    /*
     * result executes "INSERT INTO people (first_name,last_name) values ('Carol',Smith')";
     */

#### Update example

    $table               = 'people'
    $record["first_name"] = 'Carol';
    $record["last_name"]  = 'Jones';
    $where               = "last_name like 'Sm%'";

    $conn->AutoExecute($table,$record,'UPDATE', $where);

    /*
     * result executes "UPDATE people SET first_name='Carol',last_name='Jones' WHERE last_name like 'Sm%'";
     */

In situations where the table names contain certain special characters, such as dashes, the $ADODB_QUOTE_FIELDNAMES setting can be used to wrap the table and field names.

    $ADODB_QUOTE_FIELDNAMES = 'NATIVE';
    $table               = 'people'
    $record["first-name"] = 'Carol';
    $record["last-name"]  = 'Jones';
    $where               = "last_name like 'Sm%'";

    $conn->AutoExecute($table,$record,'UPDATE', $where);

    /*
     * result executes "UPDATE `people` SET `first-name`='Carol',`last-name`='Jones' WHERE `last-name` like 'Sm%'";
     */

v5/reference/connection/autoexecute.txt · Last modified: 2016/01/16 16:09 by mnewnham

### Page Tools

- Show pagesource
- Old revisions
- Backlinks
- Rename Page
- Back to top