db_query() returns a DatabaseStatementInterface object which has a function for retrieving the number of returned rows in the last SQL statement.
You could use the following piece of code to get the total number of nodes in Drupal.
$num_of_nodes = db_query('SELECT nid FROM {node}')->rowCount();
Done =)
Reference: