In part 1 of this series of articles, I discussed D7’s new database layer and we started looking at what to expect with examples of static and dynamic queries. Those queries are for fetching information from a database, but what’s changed from D6 to D7 when it comes to manipulating the information in our database? The answer: a lot.
We’ll start with INSERT queries. In D6 we were relegated to two options when it came to INSERT queries. We could either write out an INSERT query the long way:
<?php
$query = "INSERT INTO {node} (title, uid, created) VALUES ('%s', %d, %d)";
db_query($query, 'Example', 1, $timestamp);
?>
Or, we could use the function drupal_write_record():
<?php
$record = new stdClass();
$record->title = 'Example';
$record->uid = 1;
$record->created = time();
drupal_write_record('node', $record);
?>
This was a vast improvement over D5 certainly, but could still result in some cumbersome code. Looking back to the first article in this series I discussed how D7's database layer is now built on top of PDO. Let's see how we handle an INSERT query in D7 utilizing this new API.
Recent comments
14 weeks 6 days ago
19 weeks 5 days ago
22 weeks 6 days ago
1 year 8 weeks ago
1 year 9 weeks ago
1 year 9 weeks ago
1 year 15 weeks ago
1 year 15 weeks ago
1 year 17 weeks ago
1 year 17 weeks ago