->insert()
->insert() -- Insert current objects variables into database
Synopsis
mixed $DB_DataObject->insert ()
Description
Insert the data into the database, based on the variable values of the current object and returns the ID
of the inserted element if sequences or primary keys are being used. The values are correctly quoted,
and some limited type checking is done.
With mysql, the mysql_next_id() method is used, on other databases, PEAR DB sequence method is
used.
Note, insert() may not return the ID correctly in quite a few situations:
If the database backend does not support it.
The generator did not correctly flag the correct column as autoincrement/nextval
An error occured (turn on debugging to see it)
The insert failed or '0' rows where affected.
Return value
mixed - Id or key
Throws
Table 39-1. Possible PEAR_Error values
| Error code | Error message | Meaning | Solution |
|---|
| DB_DATAOBJECT_ERROR_INVALIDCONFIG | "insert:No table definition for $table" | | |
| DB_DATAOBJECT_ERROR_NODATA | "insert: No Data specifed for query" | | |
| DB_* | * | see PEAR::DB | see PEAR::DB |
Note
This function can not be called
statically.
Example
Example 39-1. Simple insert
<?php
$person = new DataObjects_Person;
$person->name='fred';
$id = $person->insert();
?>
|
|
Example 39-2. Resulting SQL
<?php
INSERT INTO person (name) VALUES ('fred');
?>
|
|