I work on D7, My Sql database name is dbdrupal7 and all table prefix with abc (abc_block, abc_node, ...), website work fine. I create a custom table without prefix abc (custom). I create a module and write to custom code.

$query = db_select('custom', 'c'); 


$results = $query->execute();



Show "unexpected Error" when browse.

I not want to change table name custom to abc_custom in database.

My question is how write correct code db_select('custom', 'c'); without prefix.


2 Answers 2


That behaviour is hard-coded with a SelectQuery; the prefix will apply to all tables, without exception, and there's no mechanism in place to override it. It happens in SelectQuery::__toString() if you're interested.

If you want to change that, you'll need to subclass SelectQuery, add a mechanism to let the class know which tables should/shouldn't be prefixed, and override the __toString() method to take that into account. Then you'll need your own version of db_select(), which uses your new custom version of the class.

All that's probably not worth the effort given that you can run an arbitrary SQL string with db_query(). For example, the following won't attempt to prefix custom:

db_query('SELECT * FROM custom c')

The "correct" way would be to treat the other tables as non-drupal databases, because they aren't (Otherwise they would be prefixed). You can see how to do this here:

How to connect to multiple databases within Drupal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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