search
Homephp教程php手册Drupal7中常用的数据库操作实例

Drupal 7 提供的新功能其中一个就是可以使用 Query Builder and Query Objects 查询生成器来构造查询对象的能力,无需在代码中写原始的SQL语句,一是提高了代码

1.插入单条记录

复制代码 代码如下:

db_insert("table")->fields(array('field1' => 'value1', 'field2' => 'value2', 'fieldn' => $valuen))->execute();

2.插入多条记录

复制代码 代码如下:

$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.更新某条记录

复制代码 代码如下:

db_update('imports')
   ->condition('name', 'Chico')
   ->fields(array('address' => 'Go West St.'))
   ->execute();
//等同于:

UPDATE {imports} SET address = 'Go West St.' WHERE name = 'Chico';


4.删除某条记录

复制代码 代码如下:

db_delete('imports')
   ->condition('name' => 'Zeppo')
   ->execute();


5.合并记录

复制代码 代码如下:

db_merge('people')
  ->key(array('job' => 'Speaker'))
  ->insertFields(array('age' => 31,'name' => 'Meredith'))
  ->updateFields(array('name' => 'Tiffany'))
  ->execute();
//如果存在job为Speaker的一条记录,则更新name为Tiffany,如果不存在,就插入一条age为31,,name为Meredith,job为Speaker的记录。

6.对数据库某字段值自动加一或者自增。

复制代码 代码如下:

db_update('example_table')
  ->expression('count', 'count + 1')
  ->condition('field1', $some_value)
  ->expression('field2', 'field2 + :inc', array(':inc' => 2))
  ->execute();

7.查询数据库某字段为另一别名(alias)

复制代码 代码如下:

$query = db_select('node', 'n');
$query->addField('n', 'name', 'label');
$query->addField('n', 'name', 'value');

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use