我希望将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";
(这个回声只是用于测试)。在这种情况下,结果是:
最大 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。
我错过了什么吗?
提前致谢!