gpt4 book ai didi

php - 将 mysql 字段的最大值分配给 php 变量

转载 作者:可可西里 更新时间:2023-11-01 00:45:02 25 4
gpt4 key购买 nike

我想将 mysql 中列的最大值分配给 PHP 中的变量。这最终将是验证表单数据。遇到麻烦,因为我是 PHP 新手。

试过这个:

$result = mysql_query("SELECT MAX(receipt_id) FROM receipts");
if (!result) {
trigger_error(mysql_error($result));
}

$row = mysql_fetch_assoc($result);
$maxid = $row["receipt_id"];

echo "The max id is $row";

( echo 仅用于测试)。在这种情况下,结果是:

最大id数组

当我在上面的代码中回显 $maxid 变量时,回显中没有返回任何值。

使用这段代码:

$result = mysql_query("SELECT MAX(receipt_id) FROM receipts");
if (!result) {
trigger_error(mysql_error($result));
}

echo "The max id is $result";

我得到:

最大 id 是资源 id #2

我在这里错过了什么?

提前致谢!

最佳答案

给max sql函数结果加一个别名:

SELECT MAX(receipt_id) AS max_value FROM receipts

像访问任何其他值一样访问它:

$row["max_value"]

关于php - 将 mysql 字段的最大值分配给 php 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21360429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com