I have inherited a drupal 6 site with some custom modules, all of which are making use of db_query() commands.

Now I'm porting the site with all it's modules to drupal 7, and consequently have to adjust most of the database queries to work with the new DBTNG abstraction layer.

That's fine, but a very tedious task. Is there any automated way to do this, or a module to at least help you on the way?

E.g.: is there some way to turn this

$query = "SELECT * FROM mytable WHERE id=1"; $result = db_query($query);

into this:

$result = db_select('mytable','t') ->fields('t') ->condition('id',1) ->execute() ->fetchAll();

Thanks a lot in advance!


The closest thing I know of to take a string db_query() value would be the Query Coder module.

Be aware of its known issues:

Known issues (taken from project page 01-29-2015):

  • INSERT queries must be specified in form "INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)"
  • Not support "->expression()" conditions for UPDATE queries.
  • All tables and fields in SELECT query must be specified with aliases, in other (UPDATE, INSERT, DELETE) queries without aliases Example: RIGHT: "SELECT * FROM users u" NOT RIGHT: "SELECT * FROM users"
  • All combining conditions in SELECT queries must be specified with aliases. Example: RIGHT: "SELECT COUNT() AS user_count FROM users u" NOT RIGHT: "SELECT COUNT() FROM users u".
  • Wow - looks great! Will check it out! Thanks a lot! Not exactly an automated port, but I guess it will save me some time :) Jan 29 '15 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.