1. Insert a single record
Copy CodeThe code is as follows: Db_insert ("table")->fields (Array (' field1 ' = ' value1 ', ' field2 ' = ' value2 ', ' fieldn ' = $valuen))- >execute ();
2. Inserting more than one record
Copy the code as follows: $values [] = Array (' field1 ' = ' val1 ', ' field2 ' = ' val2 ', ' fieldn ' = = $valn);
$values [] = Array (' field1 ' = ' value1 ', ' field2 ' = ' value2 ', ' fieldn ' = = $valuen);
$query = Db_insert (' table ')->fields (Array (' field1 ', ' field2 ', ' fieldn '));
foreach ($values as $record) {
$query->values ($record);
}
$query->execute ();
3. Update a record
Copy the Code code as follows: Db_update (' Imports ')
->condition (' name ', ' Chico ')
->fields (Array (' address ' = ' Go West St. '))
->execute ();
Equivalent to:
UPDATE {Imports} SET address = ' Go West St. ' WHERE name = ' Chico ';
4. Delete a record
Copy the Code code as follows: Db_delete (' Imports ')
->condition (' name ' = ' Zeppo ')
->execute ();
5. Merging records
Copy the Code code as follows: Db_merge (' People ')
->key (Array (' job ' = ' Speaker '))
->insertfields (Array (' age ' = ' = ' name ' = ' Meredith '))
->updatefields (Array (' name ' = ' Tiffany '))
->execute ();
If there is a record for job speaker, the update name is Tiffany, and if it does not exist, insert a record with an age of 31,name meredith,job as speaker.
6. Automatically add one or more to a database field value.
Copy the Code code as follows: Db_update (' example_table ')
->expression (' Count ', ' count + 1 ')
->condition (' field1 ', $some _value)
->expression (' Field2 ', ' Field2 +: Inc ', Array (': inc ' = 2))
->execute ();
7. Querying the database for a different alias name (alias)
Copy the code as follows: $query = Db_select (' node ', ' n ');
$query->addfield (' n ', ' name ', ' label ');
$query->addfield (' n ', ' name ', ' value ');
http://www.bkjia.com/PHPjc/736824.html www.bkjia.com true http://www.bkjia.com/PHPjc/736824.html techarticle 1. Insert a single record copy code code as follows: Db_insert ("table")-fields (' field1 ' = ' value1 ', ' field2 ' = ' value2 ', ' fieldn ' = $valuen))-ex Ecute (); 2. Insert multiple records copy Generation ...