Bibek
Bibek

Reputation: 61

How to get the max value from database in php?

$link=mysqli_connect("localhost","root","","tbl_app");
$sql="SELECT MAX(tokenno) AS max FROM tbl_order";
$result=mysqli_query($link,$sql);    

while($row=mysqli_fetch_assoc($result))
$tokenaabselect=$row['max'];
echo $tokenaabselect;

Database Design:

id   name  tokenno      
------------------
1     ram     1
2     harry   2
3     sam     6
4     ham     7
5     san     8
6     nan     9
7     hell    10

I am trying to get the maximum value from the database and i am running above code.

Now the problem, I am getting from this code is it select maximum value from tokenno as 9 where as it have to select maximum value as 10.

I don't know if the maximum value from database is 8 than it works but when maximum value on database is 10 than it select 9 or 8 the single digit highest value. I don't know what is the problem.

Please help me to select maximum value 10 from the database. Looking for positive response.

Upvotes: 0

Views: 1000

Answers (3)

Abhishek Sharma
Abhishek Sharma

Reputation: 6661

Please change you data type to int i think you datatype is string type

Upvotes: 0

Jens
Jens

Reputation: 69470

Sounds like the datatype of column tokenno is varchar or char. Change it to decimal or convert the value to decimal in your query.

SELECT MAX(CAST(tokenno AS DECIMAL(10)) AS max FROM tbl_order

Upvotes: 3

William Madede
William Madede

Reputation: 723

This is probably because of datatype of tokenno. Make sure the datatype for tokenno in your DB is not a varchar, make sure it is int for integer.

Upvotes: 0

Related Questions