[thelist] MySQL GUI Interfaces

Jay Greenspan jay at trans-city.com
Tue Mar 26 08:35:00 CST 2002


I wanted to clarify the contents of Jay's tip. I'll enclose this in a
tip as well, so the crawler picks it up.


<tip type="MySQL and PHP" author="Jay Greenspan">
Using an alias is always a good idea with functions -- aggregate or
otherwise. It's just too difficult to reference the column otherwise.
But in a case like the query below, when you're retrieving a single
value (the result of an aggregate function), you're better off using the
mysql_result() function.

For example:

$result=mysql_query("select count(*) from inventory");

echo "the result is:" . mysql_result($result,0);

It saves you a little code.

</tip>


> <tip type="MySQL & PHP" author="Jay Blanchard">
> When needing to return/print a SUM or a COUNT from a SQL query in PHP
> you
> have to give PHP a handle to grab the SUM or COUNT information so that
> it
> can be displayed or used;
>
> WRONG! :(
> SELECT COUNT(*)
> FROM tblFoo
> Does not give PHP a handle to the COUNT
>
> RIGHT! :)
> SELECT COUNT(*) AS CountOfFoo
> FROM tblFoo
> Now you can print("$row->CountOfFoo") or work with CountOfFoo as you
> need.
>
> Another example;
> SELECT SUM(TotalSales) as SalesTTL
> FROM tblFoo
> SalesTTL is the handle that PHP can work with.
>
> A NOTE OF CAUTION! - Do not name the handle the same as a column name
> with
> which you are working, the result is not pretty. Always give the
> handle a
> unique name.
> </tip>
>




More information about the thelist mailing list