从 MySQL 数据库获取 PHP 最小值?
所以我的 MySQL 数据库中有一些名称为“id”的数字。我尝试了这段代码:
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$user = $row['usrname'];
$fname = $row['fname'];
$lname = $row['lname'];
echo "<strong>User:</strong> ".$user." ".$fname." ".$lname."<br/>";
echo min($id);
}
在最后一行,我尝试回显 id 的最小值,但它不起作用。有什么建议吗?
答案:使用“ASC LIMIT 1”
while($row = mysql_fetch_assoc($sql)){
$id=$row['id'];
$user = $row['usrname'];
$fname = $row['fname'];
$lname = $row['lname'];
echo "<strong>User:</strong> ".$user." ".$fname." ".$lname."<br/>";
echo $id;
}
So I have some numbers that take the name "id" in my MySQL database. I tried this code:
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$user = $row['usrname'];
$fname = $row['fname'];
$lname = $row['lname'];
echo "<strong>User:</strong> ".$user." ".$fname." ".$lname."<br/>";
echo min($id);
}
On the last line I tried to echo the min value for id, but it didn't work. Any suggestions?
ANSWER: Use "ASC LIMIT 1"
while($row = mysql_fetch_assoc($sql)){
$id=$row['id'];
$user = $row['usrname'];
$fname = $row['fname'];
$lname = $row['lname'];
echo "<strong>User:</strong> ".$user." ".$fname." ".$lname."<br/>";
echo $id;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
Try this:
如果您无法更改查询顺序(按名称排序?),您可以使用辅助变量确定最低的 $id:
然后在循环后
print $min;
。In case you cannot change the query order (sort by names?), you can determine the lowest $id by using a helper variable:
And then
print $min;
after your loop.如果无法在 MySQL 中使用
MIN()
,请尝试此...如果您从未有任何负 ID(您不应该),您只需更改
-PHP_INT_MAX
到0
。If not able to use
MIN()
in MySQL, try this...If you never have any negative ids (you shouldn't), you could just change
-PHP_INT_MAX
to0
.