diff --git a/modules/block/block.admin.inc b/modules/block/block.admin.inc
index 358211f3c5d226852bfdeba15caca9d5c62bc99a..ba311aa0d0a3910b324f7bd05360b8814fe09930 100644
--- a/modules/block/block.admin.inc
+++ b/modules/block/block.admin.inc
@@ -101,9 +101,18 @@ function block_admin_display_form(&$form_state, $blocks, $theme = NULL) {
  */
 function block_admin_display_form_submit($form, &$form_state) {
   foreach ($form_state['values'] as $block) {
-    $block['status'] = $block['region'] != BLOCK_REGION_NONE;
+    $block['status'] = (int) ($block['region'] != BLOCK_REGION_NONE);
     $block['region'] = $block['status'] ? $block['region'] : '';
-    db_query("UPDATE {block} SET status = %d, weight = %d, region = '%s' WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $block['status'], $block['weight'], $block['region'], $block['module'], $block['delta'], $block['theme']);
+    db_update('block')
+      ->fields(array(
+        'status' => $block['status'],
+        'weight' => $block['weight'],
+        'region' => $block['region'],
+      ))
+      ->condition('module', $block['module'])
+      ->condition('delta', $block['delta'])
+      ->condition('theme', $block['theme'])
+      ->execute();
   }
   drupal_set_message(t('The block settings have been updated.'));
   cache_clear_all();
@@ -156,7 +165,10 @@ function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
     '#value' => $delta,
   );
 
-  $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title FROM {block} WHERE module = '%s' AND delta = '%s'", $module, $delta));
+  $edit = db_query("SELECT pages, visibility, custom, title FROM {block} WHERE module = :module AND delta = :delta", array(
+    ':module' => $module,
+    ':delta' => $delta,
+  ))->fetchAssoc();
 
   $form['block_settings'] = array(
     '#type' => 'fieldset',
@@ -210,7 +222,7 @@ function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
       '#type' => 'radios',
       '#title' => t('Show block on specific pages'),
       '#options' => $options,
-      '#default_value' => $edit['visibility'],
+      '#default_value' => (int) $edit['visibility'],
     );
     $form['page_vis_settings']['pages'] = array(
       '#type' => 'textarea',
@@ -221,16 +233,11 @@ function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
   }
 
   // Role-based visibility settings.
-  $default_role_options = array();
-  $result = db_query("SELECT rid FROM {block_role} WHERE module = '%s' AND delta = '%s'", $module, $delta);
-  while ($role = db_fetch_object($result)) {
-    $default_role_options[] = $role->rid;
-  }
-  $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
-  $role_options = array();
-  while ($role = db_fetch_object($result)) {
-    $role_options[$role->rid] = $role->name;
-  }
+  $default_role_options = db_query("SELECT rid FROM {block_role} WHERE module = :module AND delta = :delta", array(
+    ':module' => $module,
+    ':delta' => $delta,
+  ))->fetchCol();
+  $role_options = db_query('SELECT rid, name FROM {role} ORDER BY name')->fetchAllKeyed();
   $form['role_vis_settings'] = array(
     '#type' => 'fieldset',
     '#title' => t('Role specific visibility settings'),
@@ -261,7 +268,7 @@ function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
       t('Hide this block by default but let individual users show it.')
     ),
     '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
-    '#default_value' => $edit['custom'],
+    '#default_value' => (int) $edit['custom'],
   );
 
   $form['submit'] = array(
@@ -274,7 +281,10 @@ function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
 
 function block_admin_configure_validate($form, &$form_state) {
   if ($form_state['values']['module'] == 'block') {
-    $box_exists = (bool) db_query_range('SELECT 1 FROM {box} WHERE bid <> :bid AND info = :info', array(':bid' => $form_state['values']['delta'], ':info' => $form_state['values']['info']), 0, 1)->fetchField();
+    $box_exists = (bool) db_query_range('SELECT 1 FROM {box} WHERE bid <> :bid AND info = :info', array(
+      ':bid' => $form_state['values']['delta'],
+      ':info' => $form_state['values']['info'],
+    ), 0, 1)->fetchField();
     if (empty($form_state['values']['info']) || $box_exists) {
       form_set_error('info', t('Please ensure that each block description is unique.'));
     }
@@ -283,11 +293,29 @@ function block_admin_configure_validate($form, &$form_state) {
 
 function block_admin_configure_submit($form, &$form_state) {
   if (!form_get_errors()) {
-    db_query("UPDATE {block} SET visibility = %d, pages = '%s', custom = %d, title = '%s' WHERE module = '%s' AND delta = '%s'", $form_state['values']['visibility'], trim($form_state['values']['pages']), $form_state['values']['custom'], $form_state['values']['title'], $form_state['values']['module'], $form_state['values']['delta']);
-    db_query("DELETE FROM {block_role} WHERE module = '%s' AND delta = '%s'", $form_state['values']['module'], $form_state['values']['delta']);
+    db_update('block')
+      ->fields(array(
+        'visibility' => $form_state['values']['visibility'],
+        'pages' => trim($form_state['values']['pages']),
+        'custom' => $form_state['values']['custom'],
+        'title' => $form_state['values']['title'],
+      ))
+      ->condition('module', $form_state['values']['module'])
+      ->condition('delta', $form_state['values']['delta'])
+      ->execute();
+    db_delete('block_role')
+      ->condition('module', $form_state['values']['module'])
+      ->condition('delta', $form_state['values']['delta'])
+      ->execute();
+    $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
     foreach (array_filter($form_state['values']['roles']) as $rid) {
-      db_query("INSERT INTO {block_role} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $form_state['values']['delta']);
+      $query->values(array(
+        'rid' => $rid,
+        'module' => $form_state['values']['module'],
+        'delta' => $form_state['values']['delta'],
+      ));
     }
+    $query->execute();
     module_invoke($form_state['values']['module'], 'block_save', $form_state['values']['delta'], $form_state['values']);
     drupal_set_message(t('The block configuration has been saved.'));
     cache_clear_all();
@@ -315,18 +343,42 @@ function block_add_block_form_validate($form, &$form_state) {
  * Save the new custom block.
  */
 function block_add_block_form_submit($form, &$form_state) {
-  db_query("INSERT INTO {box} (body, info, format) VALUES ('%s', '%s', %d)", $form_state['values']['body'], $form_state['values']['info'], $form_state['values']['body_format']);
-  $delta = db_last_insert_id('box', 'bid');
-
+  $delta = db_insert('box')
+    ->fields(array(
+      'body' => $form_state['values']['body'],
+      'info' => $form_state['values']['info'],
+      'format' => $form_state['values']['body_format'],
+    ))
+    ->execute();
+
+  $query = db_insert('block')->fields(array('visibility', 'pages', 'custom', 'title', 'module', 'theme', 'status', 'weight', 'delta', 'cache'));
   foreach (list_themes() as $key => $theme) {
     if ($theme->status) {
-      db_query("INSERT INTO {block} (visibility, pages, custom, title, module, theme, status, weight, delta, cache) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, '%s', %d)", $form_state['values']['visibility'], trim($form_state['values']['pages']), $form_state['values']['custom'], $form_state['values']['title'], $form_state['values']['module'], $theme->name, 0, 0, $delta, BLOCK_NO_CACHE);
+      $query->values(array(
+        'visibility' => $form_state['values']['visibility'],
+        'pages' => trim($form_state['values']['pages']),
+        'custom' => $form_state['values']['custom'],
+        'title' => $form_state['values']['title'], 
+        'module' => $form_state['values']['module'],
+        'theme' => $theme->name, 
+        'status' => 0,
+        'weight' => 0,
+        'delta' => $delta, 
+        'cache' => BLOCK_NO_CACHE,
+      ));
     }
   }
+  $query->execute();
 
+  $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
   foreach (array_filter($form_state['values']['roles']) as $rid) {
-    db_query("INSERT INTO {block_role} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $delta);
+    $query->values(array(
+      'rid' => $rid,
+      'module' => $form_state['values']['module'],
+      'delta' => $delta,
+    ));
   }
+  $query->execute();
 
   drupal_set_message(t('The block has been created.'));
   cache_clear_all();
@@ -349,8 +401,13 @@ function block_box_delete(&$form_state, $bid = 0) {
  * Deletion of custom blocks.
  */
 function block_box_delete_submit($form, &$form_state) {
-  db_query('DELETE FROM {box} WHERE bid = %d', $form_state['values']['bid']);
-  db_query("DELETE FROM {block} WHERE module = 'block' AND delta = '%s'", $form_state['values']['bid']);
+  db_delete('box')
+    ->condition('bid', $form_state['values']['bid'])
+    ->execute();
+  db_delete('block')
+    ->condition('module', 'block')
+    ->condition('delta', $form_state['values']['bid'])
+    ->execute();
   drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
   cache_clear_all();
   $form_state['redirect'] = 'admin/build/block';
diff --git a/modules/block/block.install b/modules/block/block.install
index 6090e083516959197f90fd020afd26de6dcbf51e..7d0c30a0cbf024d16df326fdc56d58524a56f6c5 100644
--- a/modules/block/block.install
+++ b/modules/block/block.install
@@ -183,9 +183,7 @@ function block_install() {
   // during hook_page_alter(). Almost everything on the page is a block,
   // so before block module runs, there will not be much to alter.
   db_update('system')
-    ->fields(array(
-      'weight' => -5,
-    ))
+    ->fields(array('weight' => -5))
     ->condition('name', 'block')
     ->execute();
 }
diff --git a/modules/block/block.module b/modules/block/block.module
index 59a84ff929d1dd3f50f696567078a65f326f9b21..8eb78e8251a78ec2e3f11909facd2b307c0945c6 100644
--- a/modules/block/block.module
+++ b/modules/block/block.module
@@ -186,7 +186,7 @@ function block_block_list() {
   $blocks = array();
 
   $result = db_query('SELECT bid, info FROM {box} ORDER BY info');
-  while ($block = db_fetch_object($result)) {
+  foreach ($result as $block) {
     $blocks[$block->bid]['info'] = $block->info;
     // Not worth caching.
     $blocks[$block->bid]['cache'] = BLOCK_NO_CACHE;
@@ -218,7 +218,7 @@ function block_block_save($delta = 0, $edit = array()) {
  * Generates the administrator-defined blocks for display.
  */
 function block_block_view($delta = 0, $edit = array()) {
-  $block = db_fetch_object(db_query('SELECT body, format FROM {box} WHERE bid = %d', $delta));
+  $block = db_query('SELECT body, format FROM {box} WHERE bid = :bid', array(':bid' => $delta))->fetchObject();
   $data['content'] = check_markup($block->body, $block->format, '', FALSE);
   return $data;
 }
@@ -289,9 +289,10 @@ function _block_rehash() {
 
   init_theme();
 
-  $result = db_query("SELECT * FROM {block} WHERE theme = '%s'", $theme_key);
   $old_blocks = array();
-  while ($old_block = db_fetch_array($result)) {
+  $result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $theme_key));
+  foreach ($result as $old_block) {
+    $old_block = is_object($old_block) ? get_object_vars($old_block) : $old_block;
     $old_blocks[$old_block['module']][$old_block['delta']] = $old_block;
   }
 
@@ -349,14 +350,18 @@ function _block_rehash() {
   // Remove blocks that are no longer defined by the code from the database.
   foreach ($old_blocks as $module => $old_module_blocks) {
     foreach ($old_module_blocks as $delta => $block) {
-      db_query("DELETE FROM {block} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme_key);
+      db_delete('block')
+        ->condition('module', $module)
+        ->condition('delta', $delta)
+        ->condition('theme', $theme_key)
+        ->execute();
     }
   }
   return $blocks;
 }
 
 function block_box_get($bid) {
-  return db_fetch_array(db_query("SELECT * FROM {box} WHERE bid = %d", $bid));
+  return db_query("SELECT * FROM {box} WHERE bid = :bid", array(':bid' => $bid))->fetchAssoc();
 }
 
 /**
@@ -393,8 +398,14 @@ function block_box_form($edit = array()) {
 }
 
 function block_box_save($edit, $delta) {
-  db_query("UPDATE {box} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['body_format'], $delta);
-
+  db_update('box')
+    ->fields(array(
+      'body' => $edit['body'],
+      'info' => $edit['info'],
+      'format' => $edit['body_format'],
+    ))
+    ->condition('bid', $delta)
+    ->execute();
   return TRUE;
 }
 
@@ -406,11 +417,15 @@ function block_user_form(&$edit, &$account, $category = NULL) {
     $rids = array_keys($account->roles);
     $result = db_query("SELECT DISTINCT b.* FROM {block} b LEFT JOIN {block_role} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom <> 0 AND (r.rid IN (:rids) OR r.rid IS NULL) ORDER BY b.weight, b.module", array(':rids' => $rids));
     $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE);
-    while ($block = db_fetch_object($result)) {
+    foreach ($result as $block) {
       $data = module_invoke($block->module, 'block_list');
       if ($data[$block->delta]['info']) {
         $return = TRUE;
-        $form['block'][$block->module][$block->delta] = array('#type' => 'checkbox', '#title' => check_plain($data[$block->delta]['info']), '#default_value' => isset($account->block[$block->module][$block->delta]) ? $account->block[$block->module][$block->delta] : ($block->custom == 1));
+        $form['block'][$block->module][$block->delta] = array(
+          '#type' => 'checkbox',
+          '#title' => check_plain($data[$block->delta]['info']),
+          '#default_value' => isset($account->block[$block->module][$block->delta]) ? $account->block[$block->module][$block->delta] : ($block->custom == 1),
+        );
       }
     }
 
@@ -453,13 +468,7 @@ function block_form_system_performance_settings_alter(&$form, &$form_state) {
   );
 
   // Check if the "Who's online" block is enabled.
-  $online_block_enabled = db_select('block')
-    ->condition('module', 'user')
-    ->condition('delta', 'online')
-    ->condition('status', 1)
-    ->countQuery()
-    ->execute()
-    ->fetchField();
+  $online_block_enabled = db_query_range("SELECT 1 FROM {block} b WHERE module = 'user' AND delta = 'online' AND status = 1", array(), 0, 1)->fetchField();
 
   // If the "Who's online" block is enabled, append some descriptive text to
   // the end of the form description.
@@ -515,15 +524,17 @@ function block_initialize_theme_blocks($theme) {
   if (!$has_blocks) {
     $default_theme = variable_get('theme_default', 'garland');
     $regions = system_region_list($theme);
-    $result = db_query("SELECT * FROM {block} WHERE theme = '%s'", $default_theme);
-    while ($block = db_fetch_array($result)) {
+    $result = db_query("SELECT * FROM {block} WHERE theme = :theme", array(':theme' => $default_theme), array('fetch' => PDO::FETCH_ASSOC));
+    $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'custom', 'cache'));
+    foreach ($result as $block) {
       // If the region isn't supported by the theme, assign the block to the theme's default region.
       if (!array_key_exists($block['region'], $regions)) {
         $block['region'] = system_default_region($theme);
       }
-      db_query("INSERT INTO {block} (module, delta, theme, status, weight, region, visibility, pages, custom, cache) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d)",
-          $block['module'], $block['delta'], $theme, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['cache']);
+      $block['theme'] = $theme;
+      $query->values($block);
     }
+    $query->execute();
   }
 }
 
@@ -568,8 +579,23 @@ function _block_load_blocks() {
 
   $blocks = array();
   $rids = array_keys($user->roles);
-  $result = db_query(db_rewrite_sql("SELECT DISTINCT b.* FROM {block} b LEFT JOIN {block_role} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND b.status = 1 AND (r.rid IN (" . db_placeholders($rids) . ") OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", 'b', 'bid'), array_merge(array($theme_key), $rids));
-  while ($block = db_fetch_object($result)) {
+  $query = db_select('block', 'b');
+  $query->leftJoin('block_role', 'r', 'b.module = r.module AND b.delta = r.delta');
+  $result = $query
+    ->distinct()
+    ->fields('b')
+    ->condition('b.theme', $theme_key)
+    ->condition('b.status', 1)
+    ->condition(db_or()
+      ->condition('r.rid', $rids, 'IN')
+      ->isNull('r.rid')
+    )
+    ->orderBy('b.region')
+    ->orderBy('b.weight')
+    ->orderBy('b.module')
+    ->addTag('block_load')
+    ->execute();
+  foreach ($result as $block) {
     if (!isset($blocks[$block->region])) {
       $blocks[$block->region] = array();
     }
diff --git a/modules/block/block.test b/modules/block/block.test
index b6b1dd54bb761f7215a1799c3ee54a9e2c5db968..34618e92dc9aac2324baf7549702a389ebc7cfc0 100644
--- a/modules/block/block.test
+++ b/modules/block/block.test
@@ -46,7 +46,7 @@ class BlockTestCase extends DrupalWebTestCase {
 
     // Confirm that the box has been created, and then query the created bid.
     $this->assertText(t('The block has been created.'), t('Box successfully created.'));
-    $bid = db_result(db_query("SELECT bid FROM {box} WHERE info = '%s'", array($box['info'])));
+    $bid = db_query("SELECT bid FROM {box} WHERE info = :info", array(':info' => $box['info']))->fetchField();
 
     // Check to see if the box was created by checking that it's in the database..
     $this->assertNotNull($bid, t('Box found in database'));
@@ -78,7 +78,7 @@ class BlockTestCase extends DrupalWebTestCase {
     $this->drupalPost('admin/build/block/add', $box, t('Save block'));
 
     // Set the created box to a specific region.
-    $bid = db_result(db_query("SELECT bid FROM {box} WHERE info = '%s'", array($box['info'])));
+    $bid = db_query("SELECT bid FROM {box} WHERE info = :info", array(':info' => $box['info']))->fetchField();
     $edit = array();
     $edit['block_' . $bid . '[region]'] = 'left';
     $this->drupalPost('admin/build/block', $edit, t('Save blocks'));
@@ -112,7 +112,10 @@ class BlockTestCase extends DrupalWebTestCase {
     // Set block title to confirm that interface works and override any custom titles.
     $this->drupalPost('admin/build/block/configure/' . $block['module'] . '/' . $block['delta'], array('title' => $block['title']), t('Save block'));
     $this->assertText(t('The block configuration has been saved.'), t('Block title set.'));
-    $bid = db_result(db_query("SELECT bid FROM {block} WHERE module = '%s' AND delta = %d", array($block['module'], $block['delta'])));
+    $bid = db_query("SELECT bid FROM {block} WHERE module = :module AND delta = :delta", array(
+      ':module' => $block['module'],
+      ':delta' => $block['delta'],
+    ))->fetchField();
 
     // Check to see if the block was created by checking that it's in the database.
     $this->assertNotNull($bid, t('Block found in database'));