简体   繁体   English

如何从PHP中的数据库中获取最大值?

[英]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. 现在的问题是,我从这段代码中得到的是它从tokenno选择最大值为9,在这里必须将最大值选择为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. 我不知道数据库中的最大值是否大于8,但是当数据库中的最大值大于10时,它选择9或8的一位数最大值。 I don't know what is the problem. 我不知道是什么问题。

Please help me to select maximum value 10 from the database. 请帮助我从数据库中选择最大值10。 Looking for positive response. 寻找积极的反应。

Sounds like the datatype of column tokenno is varchar or char. 听起来像列tokenno的数据类型是varchar或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. 这可能是由于tokenno的数据类型。 Make sure the datatype for tokenno in your DB is not a varchar, make sure it is int for integer. 确保您的数据库中tokenno的数据类型不是varchar,确保它为整数int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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