RE: [PHP] Couple of small questions
| From: | Lawrence dot Sheed at dfait-maeci dot gc dot ca | Date: | Tue, 21 Nov 2000 01:27:35 +0000 |
| Subject: | RE: [PHP] Couple of small questions | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-26371@lists.php.net to get a copy of this message | ||
Wouldn't it be quicker to:
$SQLStatement = "SELECT SUM(Gram) FROM Purdue";
$SQLConn = mysql_connect("", "", "") or die ("Could not connect
to
database");
$db = mysql_select_db("", $SQLConn) or die ("Couldn`t select database");
$KL = mysql_query($SQLStatement) or die ("Invalid Query");
print mysql_result ($KL,0); //Only 1 result, can use array offset.
personally i use embedded if's rather than or die's but thats my style - eg:
if ($db = mysql_select_db ("",$SQLConn) {
if ... {
}
}
-----Original Message-----
From: David Robley [mailto:huntsman@hermes.nisu.flinders.edu.au]
Sent: November 21, 2000 07:55 AM
To: Website4S@aol.com; jeremy@nirvani.net
Cc: php-general@lists.php.net
Subject: Re: [PHP] Couple of small questions
On Mon, 20 Nov 2000, Website4S@aol.com wrote:
> In a message dated 20/11/2000 10:24:44 GMT Standard Time,
jeremy@nirvani.net
> writes:
>
> << select sum(Gram) from tablename;
>
> :)
> -jeremy brand >>
>
> Ok I am doing this but something still isn`t working, this is the code I
have
> anyone spot a mistake?
>
> $SQLStatement = "SELECT SUM(Gram) FROM Purdue";
>
> $SQLConn = mysql_connect("", "", "") or die ("Could
> not connect to
> database");
>
> $db = mysql_select_db("", $SQLConn) or die ("Couldn`t select
> database");
>
> $KL = mysql_query($SQLStatement) or die ("Invalid Query");
>
>
> mysql_close ($SQLConn);
> echo ($KL);
You are only getting the result pointer here in $KL. And you'll probably
need to use an alias in the query to be able to access the result of a
SUM. So:
$SQLStatement = "SELECT SUM(Gram) AS howmuch FROM Purdue";
$SQLConn = mysql_connect("", "", "") or die ("Could not connect
to
database");
$db = mysql_select_db("", $SQLConn) or die ("Couldn`t select database");
$KL = mysql_query($SQLStatement) or die ("Invalid Query");
// Only one row will be returned
$row = mysql_fetch_array($KL);
extract($row);
mysql_close ($SQLConn);
echo ($howmuch);
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net