DB_common::autoExecute()

DB_common::autoExecute() – Prepares and runs an INSERT or UPDATE query based on variables you supply

Synopsis

integer autoExecute ( string $table , array $fields_values , integer $mode = DB_AUTOQUERY_INSERT , string $where = false )

Description

Automatically prepares and executes INSERT or UPDATE queries.

This method builds a SQL statement using autoPrepare() and then executes the statement using execute() with it.

Parameter

string $table

name of the table

array $fields_values

assoc (key => value), keys are fields names, values are values of these fields

Values are automatically escaped and quoted according to the current DBMS's requirements.

integer $mode

type of query to make (DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE)

string $where

a string to be used in the WHERE clause. This is only used when $mode is DB_AUTOQUERY_UPDATE. The string is put directly into the query, so you must escape and quote literals according to the DBMS's standards.

Return value

integer - DB_OK on success or a DB_Error object on failure

Throws

Possible PEAR_Error values
Error code Error message Reason Solution
DB_ERROR_NEED_MORE_DATA insufficient data supplied Your associative array, which has to contain fields names and their values, is empty. Check and correct your fields_values array.
DB_ERROR_SYNTAX syntax error You use an unknown mode. Available modes are only DB_AUTOQUERY_INSERT for INSERT queries or DB_AUTOQUERY_UPDATE for UPDATE queries.
DB_ERROR_NODBSELECTED no database selected No database was chosen. Check the DSN in connect().
every other error code   Database specific error Check the database related section of PHP-Manual to detect the reason for this error.

Note

This function can not be called statically.

The values passed in $data must be literals. Do not submit SQL functions (for example CURDATE()). SQL functions that should be performed at execution time need to be put in the prepared statement.

Example

Using autoExecute() in insert mode

<?php
// Once you have a valid DB object named $db...
$table_name 'user';

$fields_values = array(
    
'id'      => 1,
    
'name'    => 'Fabien',
    
'country' => 'France'
);

$res $db->autoExecute($table_name$fields_values,
                        
DB_AUTOQUERY_INSERT);

if (
PEAR::isError($res)) {
    die(
$res->getMessage());
}
?>

Using autoExecute() in update mode

<?php
// Once you have a valid DB object named $db...
$table_name 'user';

$fields_values = array(
    
'country' => 'France',
);

$res $db->autoExecute($table_name$fields_values,
                        
DB_AUTOQUERY_UPDATE"country = 'Japan'");

if (
PEAR::isError($res)) {
    die(
$res->getMessage());
}
?>
Turns auto-commit on or off (Previous) Prepares an INSERT or UPDATE statement based on variables you supply (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.