resource autoPrepare (
string $table
,
array $table_fields
,
integer $mode
= DB_AUTOQUERY_INSERT
,
string $where
= false
)
Automatically builds an INSERT or UPDATE SQL statement so it can later be used by execute() or executeMultiple().
$table
name of the table
$table_fields
ordered array containing the fields names
Be aware that these fields are assigned ?
placeholders, therefore the data you pass to them in the
execute() will be automatically escaped
and quoted according to the current DBMS's requirements.
$mode
type of query to make (DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE)
$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.
resource - resource handle for the query or a DB_Error object on failure
Error code | Error message | Reason | Solution |
---|---|---|---|
DB_ERROR_NEED_MORE_DATA | insufficient data supplied | The ordered array, which has to contain fields names, is empty. | Check and correct your fields names 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. |
This function can not be called statically.
Using autoPrepare() in insert mode
<?php
// Once you have a valid DB object named $db...
$table_name = 'user';
$table_fields = array('name', 'country');
$table_values = array('Zoe', 'France');
$sth = $db->autoPrepare($table_name, $table_fields,
DB_AUTOQUERY_INSERT);
if (PEAR::isError($sth)) {
die($sth->getMessage());
}
$res =& $db->execute($sth, $table_values);
if (PEAR::isError($res)) {
die($res->getMessage());
}
?>
Using autoPrepare() in update mode
<?php
// Once you have a valid DB object named $db...
$table_name = 'user';
$table_fields = array('name', 'country');
$table_values = array('Bob', 'USA');
$sth = $db->autoPrepare($table_name, $table_fields,
DB_AUTOQUERY_UPDATE, "country = 'Japan'");
if (PEAR::isError($sth)) {
die($sth->getMessage());
}
$res =& $db->execute($sth, $table_values);
if (PEAR::isError($res)) {
die($res->getMessage());
}
?>