473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

mysql_fetch for aggregate SQL function

I don't understand how I can fetch the result for an aggregate SQL
function. I have a query written like this:

$query = "select max(qno) from answers";
$result = mysql_query($query);

$maxqno = mysql_result($result);

$nextqno = $result + 1;

I initially tried to use the row$ = mysql_fetch_array($result)
function. But, my result isn't an element in an array, corresponding
to a table column in the database. I just simply want to get the
maximum value of "qno" from the table. How do I do it?

Thanks in advance.

Sep 18 '05 #1
4 6606
$query = "select max(qno) from answers";
$result = mysql_query($query); $maxqno = mysql_result($result); $nextqno = $result + 1;


mysql_result() needs a minimum of two arguments: resource and row
number.

Alternatively, use an alias for the aggregate column and get it by
name:

$query = "select max(qno) as maxqno from answers";
$result = mysql_query( $query );

$row = mysql_fetch_array( $result, MYSQL_ASSOC );
print $row[ 'maxqno' ];

---
Steve

Sep 18 '05 #2
$query = "select max(qno) from answers";
$result = mysql_query($query); $maxqno = mysql_result($result); $nextqno = $result + 1;


mysql_result() needs a minimum of two arguments: resource and row
number.

Alternatively, use an alias for the aggregate column and get it by
name:

$query = "select max(qno) as maxqno from answers";
$result = mysql_query( $query );

$row = mysql_fetch_array( $result, MYSQL_ASSOC );
print $row[ 'maxqno' ];

---
Steve

Sep 18 '05 #3
bobkaku wrote:
$query = "select max(qno) from answers";
$result = mysql_query($query);

$maxqno = mysql_result($result);

$nextqno = $result + 1;


Sidetracking a little, are you by any chance re-implementing
auto_increment?

/m
Sep 19 '05 #4
Steve,

Thanks so much. Your solution works like a charm!
Steve wrote:
$query = "select max(qno) from answers";
$result = mysql_query($query);

$maxqno = mysql_result($result);

$nextqno = $result + 1;


mysql_result() needs a minimum of two arguments: resource and row
number.

Alternatively, use an alias for the aggregate column and get it by
name:

$query = "select max(qno) as maxqno from answers";
$result = mysql_query( $query );

$row = mysql_fetch_array( $result, MYSQL_ASSOC );
print $row[ 'maxqno' ];

---
Steve


Sep 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Claudio Lapidus | last post by:
Hello I would like to know how can I define/create a new aggregate function. I need a custom function that operate on a set of text strings and return a certain string aggregate based on certain...
1
by: sausage31 | last post by:
I have a table as follows.... Device LotID Result1 Result2 Result3 aaa 1 5 10 15 bbb 1 2 4 6 aaa 2 ...
1
by: nfrodsham | last post by:
In Microsoft's help literature, it states: "You can filter out non-unique rows by using the DISTINCT option of an aggregate function" I am trying to do this in Access 2003 with the COUNT...
10
by: neb | last post by:
Dear member of the forum, Ms access has built-in aggregate function like: -Sum, Max, First, Avg, ... Is it possible to build user-defined aggregate? (if you have any clue, do not hesitate to...
1
by: Najib Abi Fadel | last post by:
Hi i have an ordered table of dates let's say: 1/1/2004 8/1/2004 15/1/2004 29/1/2004 5/2/2004 12/2/2004
0
by: BillCo | last post by:
just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
5
by: BillCo | last post by:
I just wasted a long time figuring out this and I figure if I post it might save someone some pain! Jet (DAO) will allow you to to use nested aggregate functions like building blocks, e.g.: ...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
1
by: Sandro997 | last post by:
Ok. I have a bit of a dilemma here. First, please consider the following function: CREATE OR REPLACE FUNCTION recent_lab(text, labs, treatments) RETURNS float8 AS $BODY$select...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.