User login

Figuring out db_query()

Yeah, I use it all the time. But there's a few things about Drupal's db_query (and the database-specific MySQL etc. functions behind it, and Drupal functions like db_fetch_object() that I'd like to have very clear in my head:

  • If the query is successful but results in an empty set, is $result still a numeric reference or just zero?


$query A string containing an SQL query.

... A variable number of arguments which are substituted into the query using printf() syntax. Instead of a variable number of query arguments, you may also pass a single array containing the query arguments.
Return value

A database query result resource, or FALSE if the query was not executed correctly.

Just a side note, but that "..." for "A variable number of arguments" really drives home the case Larry Garfield has made against that setup. The function can't even define itself!

See also setting errors in Drupal (Agaric's guide in progress) for reporting SQL errors.

See also agaric's page on db_last_insert_id().


Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.