getInsertSql

See Also

$ADODB_QUOTE_FIELDNAMES
$ADODB_FORCE_TYPE
AutoExecute()

Syntax
string getInsertSql(
      mixed $recordSet,
      string[] $fieldArray,
      optional bool $placeholder=false,
      optional bool $forceType=null
     )

Description

The function getInsertSql() takes a set of parameters and returns an SQL statement that can be used to insert a record in the database. It can automatically apply any necessary database specific quoting to character fields.


Parameters

$recordSet

The parameter $recordSet is either

$fieldArray

$fieldArray is an associative key⇒value pair of fieldnames and values. The values in the pair will be inserted into the record. Note also

$ar = array('last_name'=>"John O'Reilly");

$placeHolder

This argument previosly held information about now-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.

$forceType

see $ADODB_FORCE_TYPE


Usage

/*
 * Sample uses the MySQL 'employees' demo database
 */
 
/*
 * Get the highest employee number
 */
$SQL = "SELECT emp_no FROM employees ORDER BY emp_no DESC";
$topEmployee = $db->getOne($SQL);
 
$topEmployee++;
 
/*
 * Get an empty recordset
 */
$SQL = "SELECT * FROM employees WHERE emp_no=-1";
$employeeRecord = $db->execute($SQL);
 
/*
 * Build new employee record
 */
$newEmployee = array(
    'emp_no' => $topEmployee,
    'birth_date' => $db->dbDate('1995-10-01'), //creates a portable date
    'first_name' => 'Patrick',
    'last_name' => "O'Reilly",
    'gender' => 'M',
    'hire_date' => $db->dbDate('2013-01-01')
);
 
 
$SQL = $db->getInsertSql($employeeRecord,$newEmployee);
print $SQL;
/*
 * returns: INSERT INTO employees ( EMP_NO, BIRTH_DATE, FIRST_NAME, LAST_NAME, GENDER, HIRE_DATE ) VALUES ( 500000, '1995-10-01', 'Patrick', 'O\'Reilly', 'M', '2013-01-01' )
 */
 
/*
 * Now execute SQL statement
 */
$db->execute($SQL);