ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:reference:connection:autoexecute

This is an old revision of the document!


AutoExecute

Syntax
  bool AutoExecute(
     string $tables,
     string[] $arrFields,
     string $mode,
     optional mixed $where
     optional bool $forceupdate
     optional bool $magicq
     )

$ADODB_QUOTE_FIELDNAMES provides control over the quoting and casing of fieldnames, especially useful when fieldnames contain special characters

Description

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. We generate an INSERT or UPDATE 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

StringIntegerConstant
INSERT1DB_AUTOQUERY_INSERT
UPDATE2DB_AUTOQUERY_UPDATE

$where

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

$forceupdate

ValueDescriptionOverhead
false(default). Only fields that have been modified are updatedThe database record is re-read before update
trueAll fields provided are updatedThe constructed statement may be considerably larger

$magicq

ValueDescription
falseDoes not attempt to quote special characters in character fields e.g. \0
trueAttempts to quote special characters, e.g. \0 becomes \\\0 to allow database insertion

Usage

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

Insert example

$table               = 'people'
$record["firstName"] = "Carol";
$record["lastName"]  = "Smith"; 
 
$conn->AutoExecute($table,$record,'INSERT');
 
/*
 * result executes "INSERT INTO people (firstName,lastName) values ('Carol',Smith')";
 */

Update example

$table               = 'people'
$record["firstName"] = 'Carol';
$record["lasTname"]  = 'Jones'; 
$where               = "lastname like 'Sm%'";
 
$conn->AutoExecute($table,$record,'UPDATE', $where);
 
/*
 * result executes "UPDATE people SET firstName='Carol',lasTname='Jones' WHERE lastname like 'Sm%'";
 */
v5/reference/connection/autoexecute.1437316710.txt.gz · Last modified: 2017/04/21 11:37 (external edit)