Rathan
Rathan

Reputation: 43

how to get largest number in mysql using php

I had a table named 'userinf' in mysql containing account number (acno) which is auto_incrementing to retrieve largest of acno i used the below command.

qr="select MAX('acno') from 'userinf' ";

$EX=mysql_query($qr) or die(mysql_error());

$ex=mysql_fetch_ARRAY($EX);

echo "$ex";

and want iam getting is the name of the column i.e acno but not maximum value

Upvotes: 0

Views: 116

Answers (7)

Vijay
Vijay

Reputation: 8451

try out this..

mysql_fetch_ARRAY returns array of result...

qr="select MAX(acno) as acn from userinf ";

$EX=mysql_query($qr) or die(mysql_error());

$ex=mysql_fetch_ARRAY($EX);

echo $ex[0];

Upvotes: 0

Amit Garg
Amit Garg

Reputation: 3907

qr="select MAX(`acno`) as acn from `userinf` ";

$EX=mysql_query($qr) or die(mysql_error());

$ex=mysql_fetch_ARRAY($EX);

echo $ex['acn'];

Do not use single quote' use back tick ` it is near by tab button.

Upvotes: 1

Goutam Pal
Goutam Pal

Reputation: 1763

$ex=mysql_fetch_ARRAY($EX);

echo "$ex"

should be

$ex=mysql_fetch_array($EX);

echo $ex[0];

Upvotes: 1

Ayyappan Sekar
Ayyappan Sekar

Reputation: 11475

Try

 `echo $ex[0]`

or you can use

`echo $ex['max_acc_no']`

if you use

`select MAX('acno') max_acc_no from 'userinf'` 

as the query

Upvotes: 1

Goutam Pal
Goutam Pal

Reputation: 1763

qr="select MAX('acno') from 'userinf' ";

$EX=mysql_query($qr) or die(mysql_error());

$ex=mysql_fetch_ARRAY($EX);

echo "$ex";

should be like this

$qr="select MAX('acno') as acno from 'userinf' ";

$EX=mysql_query($qr) or die(mysql_error());

$ex=mysql_fetch_array($EX);

echo $ex['acno'];

or 
echo $ex[0];

Upvotes: 1

silkfire
silkfire

Reputation: 25935

Try this:

list($ex) = mysql_fetch_num($EX);
echo $ex;

Upvotes: 0

user4035
user4035

Reputation: 23719

1.Change the query, removing quotes:

qr="select MAX(acno) from userinf";

Field names are quoted, using backqoutes: ` in MySQL, not straight quotes, that you use. But you don't need it here.

2.Remember, that you are receiving the array, so you must use an index:

echo "$ex[0]";

Upvotes: 1

Related Questions