db_affected_rows gives fatal error in drupal 7
There is no db_affected_rows() in Drupal 7 I'm afraid. Also you don't need include the quotes in your variable placeholder. Your code might look a little more like this:

$results = db_query("SELECT * FROM {template_config} WHERE user_role = :role", array(':role' =&gt; 'the_role'))-&gt;fetchAll();

if (count($results) &gt; 0) {
  $first_result = $results[0];

  $custom_theme = $first_result-&gt;theme_name;
}


It could be even simpler though:

if ($result = db_query("SELECT * FROM {template_config} WHERE user_role = :role", array(':role' =&gt; 'the_role'))-&gt;fetchObject()) {
  $custom_theme = $result-&gt;theme_name;
}