string getUpdateSql( obj $result, string[] $fieldArray, optional bool $forceUpdate=false, optional bool $placeHolder=false, optional bool $forceType=null )
The function getUpdateSql()
takes a set of parameters and returns an SQL statement that can be used to update a record in the database. It can automatically apply any necessary database specific quoting to character fields and exclude any unchanged variables.
The parameter $result
is the handle to the result obtained by executing a select statement to obtain the record, not the recordset associated with the request. There is no need to read the row.
$SQL = "SELECT * FROM employees WHERE emp_no=10001"; $result = $db->execute($SQL); $sql = $db->getUpdateSql($result,.....
$fieldArray
is an associative key⇒value pair of fieldnames and values. The values in the pair will replace the values in the existing record
$ar = array('actname'=>"John O'Reilly");
$forceUpdate
forces the sql statement to include updates for fields that have not changed. It may be necessary to force updates when, for example, distinction needs to be made between null and zero in integer fields.
In addition, use of this parameter eliminates the need to add additional testing to determine if any fields have changed. In cases such as these, if the parameter is not set and there have been no modified fields, the getUpdateSql() method will return an empty string, causing execute to fail if passed this parameter.
This argument was previously used for information about deprecated PHP functionality, and is now ignored
This parameter was designed to provide backwards compatibility with now unsupported versions of ADOdb and may be removed in future releases.
/* * Sample uses the MySQL 'employees' demo database */ $SQL = "SELECT * FROM employees WHERE employee_no=10001"; $employeeRecord = $db->getAll($SQL); /* * Employee record returns: Array ( [emp_no] => 10001 [birth_date] => 1953-09-02 [first_name] => Georgi [last_name] => Facello [gender] => M [hire_date] => 1986-06-26 ) */ $SQL = 'SELECT * FROM employees WHERE emp_no=10001'; $result = $db->execute($SQL); $updateArray = array('first_name'=>'Jerry', 'last_name'=>"O'Reilly", 'gender'=>'M'); $SQL = $db->getUpdateSql($result, $updateArray); /* * returns * UPDATE employees SET FIRST_NAME='Jerry', LAST_NAME='O\'reilly' WHERE emp_no=10001 */
Note:
last_name
O'Reilly was automatically quoted into an appropriate formatted for insertion into a MySQL database.gender
field was unchanged, it was discarded by the update statement.
If we want the returned statement to include all the supplied fields, we add the $forceUpdate
option.
$SQL = $db->getUpdateSql($result, $updateArray, true); /* * returns * UPDATE employees SET FIRST_NAME='Jerry', LAST_NAME='O\'reilly', GENDER='M' WHERE emp_no=10001 */