0

I am new to drupal. I am trying to create a function that will retrieve rows from a database and display the results.From the tutorials I have read, I am trying to implement a function I have created but no results are displayed. There's just a whitespace. Where might I be going wrong? Here is my code.

function print_users(){
$query = "SELECT first_name, middle_name, last_name FROM {registered_users}";
$header = array(
array('data' => t('Cell 1'), 'field' => 'first_name', 'sort' => 'ASC'),
array('data' => t('Cell 2'), 'field' => 'middle_name'),
array('data' => t('Cell 3'), 'field' => 'last_name'),
);
$q = db_query($query . tablesort_sql($header));
while ($r = db_fetch_array($q)) {
    $rows[] = $r;
}
print theme('table', $header, $rows);

}

and am calling it as follows via a menu link

 $items['view_users'] = array(
        'title' => "view",
        'description' => 'Viewing service',
        'page callback' => "print_users",
        'access callback' => TRUE, 
        'weight' => '2',

);

2
  • why {registered_users}? What is the query you execute here db_query($query . tablesort_sql($header))? Could you print it and try directly on phpmyadmin to see if it makes sense? Btw Why not db_query($query);? if you want to add sorting or other attributes you can try dynamic queries drupal.org/node/310075. Commented Aug 16, 2013 at 10:37
  • If something is wrong, it is customary to provide error message or description why result does not satisfy your needs.
    – Mołot
    Commented Aug 26, 2013 at 14:52

2 Answers 2

1
$select = db_select('registered_users', 'c')
->condition('c.fieldname', $id)  //try the condition if u have anything
->fields('c');

$result = $select->execute();
if($result->rowCount() == 0) return false;

$val = $result->fetch();

for sorting try asc or desc followed by the condition you can get the answer in google itself

0

The title mentions Drupal 7, but db_fetch_array() no longer exists in 7.x so you want to make sure what version you're actually using, and that you're not wasting your time with tutorials for previous versions.

Also, looking at the last line of your function:

 print theme('table', $header, $rows);

A Drupal function called from the menu does not print directly its HTML output, it must return it as a string, so that the caller combines it with the rest of the page.

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.