making a database enquiry using db_query

a great place f... - March 12, 2008 - 12:39

I'm very new to php and the inner workings of drupal and I need some help with this one.

I'm trying to amend the teaser_images.module to not work for a certain content type.
I'm trying to get the content type from the data base with

$result = (db_query('SELECT type FROM node_type'));

and I end up with 'Resource id #nnn' in my $result variable regardless of the query, I can select from a different table and get the same result.

Do I need to connect first? use a different function? I've looked through the documentation and I'm stumped.

In this case you should probably be calling node_get_types()

nevets - March 12, 2008 - 14:47

In this case you should probably be calling node_get_types().

As for access the database, db_query returns a result set so you need to loop through the results (which could be empty). In general this logic looks something like

$sql = "some sql statement";
$results = db_query($sql);
while ( $data = db_fetch_object($results) ) {
  // Can now access fields using $data
  // For example if the query returns a field called
  // state you could access it with $data->state
}

If you prefer working with arrays over objects you can use db_fetch_array() instead of db_fetch_object().

 
 

Drupal is a registered trademark of Dries Buytaert.