[REVIEW] database->insertObject

Last reviewed: Not reviewed
phpDocumentor

database->insertObject

Inserts a new database table row using data from an object.  Returns true if the insertion was successful; false otherwise.

Syntax

boolean insertObject ( string $table, object &$object [, string $keyname [, boolean $verbose ]] )

$table
is the name of the database table.
$object
is an object containing the database fields to be inserted.  Attributes that are arrays or objects, have a null value or have a name beginning with an underscore, are ignored when setting the database fields.
$keyname
is the primary keyname of the database table. If this is not null then the $keyname attribute of $object will be updated with the unique id number returned by the database insertion. This parameter is optional and if omitted defaults to null.
$verbose
is a flag. If true then the SQL query and the unique id returned by the query are output. If false then no output is produced. This parameter is optional and if omitted defaults to false.

Examples

Example: This example inserts a new row in the Joomla users table.  Notice that the 'id' field is set to null before calling insertObject and will be updated with the unique id number allocated by the database.

// Create new row in users table and return the unique id allocated
// if successful or false otherwise.
function newUser( $name, $username) {
  global $database;
 
  $user = new stdClass;
  $user->id = NULL;
  $user->name = $name;
  $user->username = $username;
 
  if (!$database->insertObject( '#__users', $user, 'id' )) {
    echo $database->stderr();
    return false;
  }
 
  return $user->id;
}
 
echo newUser( 'Linus Torvalds', 'linus' );

Please report any errors on this page to the Developer Documentation Forum.

Last Updated ( Monday, 25 December 2006 )