[support] Drupal 5 db_query

Larry Garfield larry at garfieldtech.com
Mon Sep 22 22:04:57 UTC 2008


http://api.drupal.org/api/function/db_fetch_array/5

Drupal's db_fetch_array functions use *_fetch_assoc() internally.  In ext/mysql and ext/pgsql, *_fetch_array() returns a doubly indexed array by field AND number.  *_fetch_assoc() returns just the associative array.  I have frankly never seen a reason to want the doubly indexed version, so it's good that Drupal gives you the version that makes sense. :-)

You should be opposed to using mysql_* directly, as that is the Wrong Way(tm) to access the database.  That bypasses all of the Drupal security handling and is an SQL Injection risk.  

In this case, in fact, you probably don't want a fetch at all.  You just want the single count value.

$count = db_result(db_query($countsql));

And you're done.

--Larry Garfield

On Mon, 22 Sep 2008 13:06:14 +0200, "Fred Jones" <fredthejonester at gmail.com> wrote:
> Here is some trivial code:
> 
> $n= db_query($countsql) ;
> echo "<!-- ";print_r(db_fetch_array($n)); echo " -->";
> $countsql = str_replace('{','',$countsql);
> $countsql = str_replace('}','',$countsql);
> $resultXXX = mysql_query($countsql);
> echo "<!-- ";print_r(mysql_fetch_array($resultXXX)); echo " -->";
> 
> which runs my $countsql through db_query, shows the result and then
> through mysql_query and shows the results. The two other lines just
> remove the { and } characters. I would have expected to see the same
> results both times, but what I see is:
> 
> <!-- Array
> (
>     [COUNT(*)] => 0
> )
>  --><!-- Array
> (
>     [0] => 4
>     [COUNT(*)] => 4
> )
>  -->
> 
> The mysql_query version returns the correct result--that is what I see
> if I run the query via phpMyAdmin as well. Seems that Drupal's
> db_query is doing something to it. The operative issue is the use of
> UCASE I am quite certain. I wanted to make my LIKE phrase case
> insensitive, so I added UCASE to both sides of it, but now db_query
> fails.
> 
> Anyone have any feedback on this? I would not be opposed to just using
> mysql_query but the query in question I actually want to pass to
> pager_query, which in turn of course passes it to db_query.
> 
> Thanks.
> --
> [ Drupal support list | http://lists.drupal.org/ ]



More information about the support mailing list