fields(array('field1' => 'value1', 'field2' => 'value2', 'fieldn' => $valuen))->execut">
时间:2021-07-01 10:21:17 帮助过:3人阅读
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');
http://www.bkjia.com/PHPjc/736824.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/736824.htmlTechArticle1.插入单条记录 代码如下:db_insert("table")-fields(array('field1' = 'value1', 'field2' = 'value2', 'fieldn' = $valuen))-execute(); 2.插入多条记录 复制代...