This is a simple feature that I want to be able to use many times, detect if a record exists or not. I know how to do this in regular PHP and MySQL but not in Drupal's Database Abstraction Layer. There has been many times I wanted to do some PHP hacks but I'm trying to do things the Drupal way so future developers of my site don't have to learn Drupal and my hacks.

Comments

db_query() take a SQL

db_query() take a SQL statement so you can use that to run a SELECT query.

This is what I have...

This is what I have

<?php
    $count
= db_result(db_query('SELECT * FROM {cdn_webfm_ext} WHERE uid = %d', $user->uid));
?>

My understanding is this is supposed to return 0 because the table is empty or the record dose not exist. It returns nothing.

This returns "Resource id #109", what the heck?

<?php
$result
= db_query('SELECT * FROM {cdn_webfm_ext} WHERE uid = %d', $user->uid);
?>

That's because the second one

That's because the second one is a mysql result. It doesn't become a value until you use db_fetch_array() or db_fetch_object() to extract the data from the resource.

Anyways, to find if something exists, you can use this:

<?php
    $exists
= db_result(db_query('SELECT 1 FROM {cdn_webfm_ext} WHERE uid = %d', $user->uid));
?>

$exists will be true if the record exists, false if it doesn't.

Full-time freelancer, always looking for work.
jaypan.com (my portfolio)

Thanks!

Thanks! It worked.

nobody click here