Skip to main content
1 of 2
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Your code has the following errors:

  • The string delimiter for the query is the single quote character, which is the same delimiter you use for the string inside the query.
  • The table names are always surrounded with { and }, which allows Drupal to prepend the prefix set in settings.php to the table name.
  • If you need to limit the number of returned rows, in Drupal you use db_query_range() that allows to use a query that is compatible with every database engine for which there is a driver for Drupal. If you look at the code used from that function for different database engines, you will notice it will use a different syntax for different database engines, such as:

###MySQL/MySQLi

    $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
    $query .= ' LIMIT ' . (int) $from . ', ' . (int) $count;

###PostGreSQL

    $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
    $query .= ' LIMIT ' . (int) $count . ' OFFSET ' . (int) $from;

Your code should be written as the following one:

$sql = "SELECT name, mail FROM {users} WHERE status = 1 AND mail LIKE '%@hotmail%'";
$result = db_query_range($sql, array(), 0, 25);

while ($row = db_fetch_object($result)) {
  // $row->name contains the username, and $row->main contains the email address.
}
avpaderno
  • 97.9k
  • 15
  • 164
  • 284