ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:reference:connection:getupdatesql

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
reference:getupdatesql [2015/07/25 16:15] – created mnewnhamv5:reference:connection:getupdatesql [2023/09/26 17:43] (current) – Fix broken link to execute() page dregad
Line 1: Line 1:
 ====== getUpdateSql ====== ====== getUpdateSql ======
 +~~NOTOC~~
 <WRAP right box> <WRAP right box>
 == See Also == == See Also ==
-[[reference:adodb_quote_fieldnames|$ADODB_QUOTE_FIELDNAMES]]+[[v5:reference:adodb_quote_fieldnames|$ADODB_QUOTE_FIELDNAMES]]
  
 == Syntax == == Syntax ==
   string getUpdateSql(   string getUpdateSql(
-        mixed $recordSet,+        obj $result,
         string[] $fieldArray,         string[] $fieldArray,
         optional bool $forceUpdate=false,         optional bool $forceUpdate=false,
-        optional bool $magicQuotes=false, +        optional bool $placeHolder=false, 
-        optional bool $forceType=false+        optional bool $forceType=null
        )        )
 </WRAP> </WRAP>
 ===== Description ===== ===== Description =====
 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 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.
 +------------------
 ===== Parameters ===== ===== Parameters =====
-====  $recordSet ==== +====  $result==== 
-The parameter ''$recordSet'' is __either__ a string holding the name of a table, __or__ a recordset obtained by [[reference:execute|executing]] a select statement to obtain record. +The parameter ''$result'' is the handle to the result obtained by [[v5:reference:connection:execute|executing]] a select statement to obtain the record, **not the recordset associated with the request**. There is no need to read the row
-=== Acceptable Methods Of Passing Parameter 1 ===+
 <code php> <code php>
-$table = 'ACT'; +$SQL = "SELECT * FROM employees WHERE emp_no=10001";
-$sql = $db->getUpdateSql($table,..... +
-</code> +
-or +
-<code php> +
-$SQL = "SELECT * FROM act WHERE actno=10";+
 $result = $db->execute($SQL); $result = $db->execute($SQL);
 $sql = $db->getUpdateSql($result,..... $sql = $db->getUpdateSql($result,.....
 </code> </code>
-=== Uncceptable Methods Of Passing Parameter 1 ===+==== $fieldArray ==== 
 +''$fieldArray'' is an associative key=>value pair of fieldnames and values. The values in the pair will replace the values in the existing record
 <code php> <code php>
- +$ar array('actname'=>"John O'Reilly");
-$sql $db->getUpdateSql('ACT',.....+
 </code> </code>
-or+-------------------------------------------------------------------------------- 
 +==== $forceUpdate ==== 
 +''$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 [[v5:reference:connection:execute]] to fail if passed this parameter. 
 +------------------------------------------------------------------------------- 
 +==== $placeHolder ==== 
 +This argument was previously used for information about deprecated PHP functionality, and is now ignored  
 +-------------------------------------------------------------------------------- 
 +<WRAP right tip 300px> 
 +This parameter was designed to provide backwards compatibility with now unsupported versions of ADOdb and may be removed in future releases. 
 +</WRAP> 
 +==== $forceType ==== 
 + 
 +see [[v5:reference:adodb_force_type|$ADODB_FORCE_TYPE]] 
 +------------------------------------------- 
 +===== Usage =====
 <code php> <code php>
-$SQL = "SELECT * FROM act WHERE actno=10"; +/* 
-$result = $db->selectLimit($SQL,1); + * Sample uses the MySQL 'employees' demo database 
-$sql = $db->getUpdateSql($result,.....+ */ 
 + 
 +$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 
 + */
 </code> </code>
 +Note:
 +  *  The ''last_name'' **O'Reilly** was automatically quoted into an appropriate formatted for insertion into a MySQL database.
 +  * Because the value of the ''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.
  
-==== $fieldArray ====+<code php> 
 +$SQL = $db->getUpdateSql($result, $updateArray, true); 
 +/* 
 + * returns 
 + * UPDATE employees SET FIRST_NAME='Jerry',  
 +                        LAST_NAME='O\'reilly',  
 +                        GENDER='M'  
 +                        WHERE emp_no=10001 
 + */ 
 +</code>
  
  
  
v5/reference/connection/getupdatesql.1437833710.txt.gz · Last modified: 2017/04/21 11:34 (external edit)