Displaying table data from DB by sql query

jeremydr - September 13, 2006 - 05:25

Hi,

I have a question about displaying data from one of the tables in DB in my web page, is it possible with drupal?

for example:
I have a DB table named Students and I would like to display in one of my pages all the
students that are registered to a course (and all their details as well).
in order to do that I need to query mysql and present result in some kinf of table.

I read something about the Table Manager module, any ideas?

thanks for the help,
Jeremy

...

mooffie - September 13, 2006 - 08:08

TableManager manages its own tables. You, OTOH, have an independent table, with a particular structure, and possibly updated regularly by some other application.

is it possible with drupal?

Everything is possible when you know some PHP.

You can enter PHP code into your node (either directly, when you choose the "PHP code" input format; or indirectly, when you use, e.g., the macrotags module). Don't fret: it's easier than it sounds, and we're here to help.

The basic PHP code you use is:

<?php
  $header
= array('ID', 'Name', 'Age');
 
$rows = array();

 
$sql = 'SELECT studentID, name, age FROM {students} ORDER BY name';
 
$res = db_query($sql);
  while (
$row = db_fetch_array($res)) {
   
$rows[] = $row;
  }

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

Tweak this code to suit your particular 'students' table, paste it into a node, choose the "PHP code" input format (visible only when you're Admin), and hit "Submit". (Later you may want to create some macrotag tags for this code.)

(BTW, you may want to enable your contact form.

Thanks for your answer, I

jeremydr - September 14, 2006 - 12:29

Thanks for your answer, I tried it and it works.

Thanks

ndvo - September 27, 2009 - 14:41

Your post really helps php noobs like myself.

 
 

Drupal is a registered trademark of Dries Buytaert.