数据插入更新记录函数drupal_write_record()

-- Submitted by Admin on Sun, 05/30/2010 - 09:26

有了drupal_write_record()函数,还需要我们自己写SQL语句嘛?这正是为“不习惯”用sql的人群准备的API。

drupal_write_record($table, &$object, $update  = array())

示例:

<?php
$table
= 'table_name'//要插入或更新数据的表名
$record = new stdClass();
$record->name = 'liuyun';//搜集对象数据
drupal_write_record($table, $record);//插入一条记录

$record->name = 'yunliu';
$record->lid = 1;
drupal_write_record($table, $record, 'lid');//更新一条记录
?>

注意,若是新增一条记录,就不必有$update参数了,若是编辑一条记录保存,即更新,则要加上第三个参数,而这个参数就是数据表的主键值 primary key,因为主键值是各不相同的,所以作为替换更新的标识。

Drupal真的想的很周到啊!

Comments

顶一下!~

顶一下!~

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <em> <strong> <code> <cite> <ul> <ol> <li> <dl> <dt> <dd> <img> <ebmed>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
16 + 4 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.