简体   繁体   中英

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.

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

请更改您的数据类型为int我认为您的数据类型是字符串类型

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM