ADOdb Library for PHP Manual
Prev AutoExecute/GetUpdateSQL Next

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

Since ADOdb 4.56, you can automatically generate and execute INSERTs and UPDATEs on a given table with this function, which 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).

Legal values for $mode are

You have to define the constants DB_AUTOQUERY_UPDATE and DB_AUTOQUERY_INSERT yourself or include adodb-pear.inc.php.

The $where clause is required if $mode == 'UPDATE'. If $forceUpdate=false then we will query the database first and check if the field value returned by the query matches the current field value; only if they differ do we update that field.

Returns true on success, false on error.

An example of its use is:

$record["firstName"] = "Carol";
$record["lasTname"] = "Smith"; 
$conn->AutoExecute($table,$record,'INSERT');
# executes "INSERT INTO $table (firstName,lasTname) values ('Carol',Smith')";

$record["firstName"] = "Carol";
$record["lasTname"] = "Jones"; 
$conn->AutoExecute($table,$record,'UPDATE', "lastname like 'Sm%'");
# executes "UPDATE $table SET firstName='Carol',lasTname='Jones' WHERE lastname like 'Sm%'";

Note: One of the strengths of ADOdb's AutoExecute() is that only valid field names for $table are updated. If $arrFields contains keys that are invalid field names for $table, they are ignored. There is some overhead in doing this as we have to query the database to get the field names, but given that you are not directly coding the SQL yourself, you probably aren't interested in speed at all, but convenience.

Since 4.62, the table name to be used can be overridden by setting $rs->tableName before AutoExecute(), GetInsertSQL() or GetUpdateSQL() is called.

GetUpdateSQL(&$rs, $arrFields, $forceUpdate=false,$magicq=false,$forcenulls=false)

Generate SQL to update a table given a recordset $rs, and the modified fields of the array $arrFields (which must be an associative array holding the column names and the new values) are compared with the current recordset. If $forceUpdate is true, then we also generate the SQL even if $arrFields is identical to $rs->fields. Requires the recordset to be associative. $magicq is used to indicate whether magic quotes are enabled (see qstr()).

Since 4.52, we allow you to pass the $force type parameter, and this overrides the $ADODB_FORCE_TYPE global variable.

Since 4.62, the table name to be used can be overridden by setting $rs->tableName before AutoExecute(), GetInsertSQL() or GetUpdateSQL() is called.


Prev Home Next
Replace Up GetInsertSQL

Sponsored by phpLens