Examples of database operations commonly used in DRUPAL7 _php tutorials

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.