从 MySQL 数据库获取 PHP 最小值?

发布于 2024-10-30 22:52:26 字数 663 浏览 2 评论 0原文

所以我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

硬不硬你别怂 2024-11-06 22:52:26
$sql = mysql_query("SELECT * FROM users ORDER BY 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;
} 
$sql = mysql_query("SELECT * FROM users ORDER BY 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;
} 
深居我梦 2024-11-06 22:52:26

试试这个:

$sql = 'SELECT min(id) as min FROM users';
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$min_id = $row['min'];

Try this:

$sql = 'SELECT min(id) as min FROM users';
$query = mysql_query($sql);
$row = mysql_fetch_assoc($query);
$min_id = $row['min'];
高跟鞋的旋律 2024-11-06 22:52:26

如果您无法更改查询顺序(按名称排序?),您可以使用辅助变量确定最低的 $id:

 $min = isset($min) ? min($min, $id) : $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:

 $min = isset($min) ? min($min, $id) : $id;

And then print $min; after your loop.

念三年u 2024-11-06 22:52:26

如果无法在 MySQL 中使用 MIN(),请尝试此...

$smallestId = -PHP_INT_MAX;

while($row = mysql_fetch_assoc($sql)) {
   ...
   $smallestId = min($smallestId, $id);
}

如果您从未有任何负 ID(您不应该),您只需更改 -PHP_INT_MAX0

If not able to use MIN() in MySQL, try this...

$smallestId = -PHP_INT_MAX;

while($row = mysql_fetch_assoc($sql)) {
   ...
   $smallestId = min($smallestId, $id);
}

If you never have any negative ids (you shouldn't), you could just change -PHP_INT_MAX to 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文