Im using a custom query to display some content. This query uses url arguments as MySQL variables.

Usually when I create a link with the variables the page does not exist and it returns a 404 error.

Is there a way of creating a page on certain urls to avoid getting an "page not found"?

EDIT: To be more specific

The query is something like

SELECT * WHERE node.type = '.arg(0).' AND node_data_field_type.field_type = '.arg(1).'

The url intended would be: example.com/exhibition/speacial_feature or example.com/program/speaker_series

The link exhibition/speacial_feature or program/speaker_series are not found.

  • 1
    can you give some examples of the urls you are creating? this might help out in resolving your problem
    – Jimajamma
    Commented Aug 6, 2012 at 5:09
  • 4
    Note that your query is, if really done like that, insecure and open for SQL-Injection. Use placeholders! (see db_query() documentation).
    – Berdir
    Commented Aug 8, 2012 at 22:42
  • Ouch, no query placeholders, SQL args from querystring. Commented Feb 1, 2013 at 23:28

2 Answers 2


Supposing that example.com is a site running Drupal, what Drupal does when you try to access example.com/exhibition/speacial_feature is:

  • Verify exhibition/speacial_feature is a path alias
  • Verify there is a menu that uses exhibition/speacial_feature as path
  • Verify a module define a menu callback for exhibition/speacial_feature

If none of the conditions is verified, then Drupal returns a 404 error.

In your case, as you want to execute code when a path is entered, you need to create a module that defines a menu callback.

function mymodule_menu() {
  $items = array();

  $items['mymodule/%/%'] = array(
    'page callback' => 'mymodule_abc_view',
    'page arguments' => array(1, 2),

  return $items;

function mymodule_page_view($first, $second) {
  // Execute the query, and show the result.

The code for hook_menu() I shown is the minimum required; see hook_menu() for more information.

As also noted from Berdir, you don't concatenate input given from a user to obtain the query to execute, as that causes SQL injection. You need to use query parameters, which are supported in any Drupal version. Also, with static queries, the name of the database tables are surrounded by { and }.


When a request is made to a Drupal site, it looks up in the menu router and the url table to see if that link exists. Since you are using a custom query, it will fail to recognize your new pattern. You can either create a normal node, a URL alias as per your requirements, and change the content in the node.tpl.php file.

Otherwise, you can create a menu. Say your custom URL is foobar/X, where X is varying.

 $items['foobar/%'] = array(
   'page callback' => 'foo_page',
   'page arguments' => array(1, 'foo'),

Then, in the page callback, fetch the custom content and return it.

  • This is probably it, unfortunately I don't know how to apply or test it. Could you elaborate with more detail? Excuse my ignorance but I don't know exactly what the callback function is or where to run it.
    – Pedro
    Commented Aug 8, 2012 at 20:31

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.