[development] Programmatically creating SQL queries
andy at pedalo.co.uk
Sat Mar 12 09:59:54 UTC 2011
Thanks John and Larry,
> db_query will take an array of arguments -
> db_query("SELECT blah...", 1, 2, 3) and
> db_query("SELECT blah, array(1, 2, 3)) are the same thing.
> In Drupal 7, there is a dynamic query builder built right into the system
> You should also look into this function if you need to build, say, an IN
Thanks, I would've done that manually if you hadn't pointed it out.
. . . . . . .
+44 (0)20 8747 5068
andy at pedalo.co.uk
This email is intended only for the above named addressee/s. This email may be confidential or legally privileged. If you have received this email and you are not a named addressee, you must not use, copy, distribute or disclose the email or any part of its contents or take any action in reliance on it. If you have received this email in error, please email the sender by replying to this message and delete it from your system. All reasonable precautions have been taken to ensure no viruses are present in this email.
pedalo limited cannot accept responsibility for loss or damage arising from the use of this email or attachments and recommends that you subject these to your virus checking procedures prior to use. Any views or opinions presented are solely those of the author and not necessarily those of Pedalo Limited
Please consider the environment before printing this email
From: development-bounces at drupal.org [development-bounces at drupal.org] On Behalf Of Larry Garfield [larry at garfieldtech.com]
Sent: 12 March 2011 06:33
To: development at drupal.org
Subject: Re: [development] Programmatically creating SQL queries
The answer depends heavily on your Drupal version.
In Drupal 7, there is a dynamic query builder built right into the system for
exactly that purpose:
In Drupal 7, that is the one and only way you should be building dynamic
queries. (But if you don't need a dynamic query, just use db_query() as it is
In Drupal 6, as another responder said you need to build the SQL string
yourself and an array of placeholder values, then call db_query() with its
$sql = "...";
$values = array(1, 2, 3);
$result = db_query($sql, $values);
You should also look into this function if you need to build, say, an IN
(That will wrap in your mail client. Sorry.)
That is the only way you should be building dynamic queries.
 Except for a few very strange edge cases where you may need to do
something else, but odds are if you hit one of those you did something else
More information about the development