从 PHP 调用的 MySQL MAX(id) 产生奇怪的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3137925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL MAX(id) called from PHP produces strange value
提问by ubiquibacon
I am just trying to get the auto incremented value of a table that is currently the highest. I do not need to know what the next auto increment is, just the highest value of what is in the table right now. I am using the code below, but regardless of what the actual auto increment is, what table I last inserted into, what table was last updated / modified, or any other factors that I can see, the value always returns Resource id #4. This is perplexing to me for two reasons. First I don't understand why the number is always 4, second I do not understand why I am getting back a string value (with letters and a symbol) instead of just an integer. What is the deal here?
我只是想获取当前最高的表的自动递增值。我不需要知道下一个自动增量是什么,只需要知道现在表中的最大值。我正在使用下面的代码,但无论实际的自动增量是什么、我上次插入的表、上次更新/修改的表或我可以看到的任何其他因素,该值总是返回Resource id #4. 出于两个原因,这让我感到困惑。首先我不明白为什么数字总是4,其次我不明白为什么我要返回一个字符串值(带有字母和符号)而不仅仅是一个整数。 这里有什么交易?
<?php $highest_id = mysql_query("SELECT MAX(c_id) FROM customers"); ?>
<?php $highest_id = mysql_query("SELECT MAX(c_id) FROM customers"); ?>
回答by Amber
mysql_querydoesn't return the value from the query, it returns a result resource. To get the actual value, you need to use one of the mysql_fetch_*functions, passing it the result resource you got from mysql_query.
mysql_query不返回查询的值,它返回一个结果资源。要获取实际值,您需要使用其中一个mysql_fetch_*函数,将您从mysql_query.
<?php
$result = mysql_query("SELECT MAX(c_id) FROM customers");
$row = mysql_fetch_row($result);
$highest_id = $row[0];
?>
or the shorter...
或者更短的...
<?php
$highest_id = mysql_result(mysql_query("SELECT MAX(c_id) FROM customers"), 0);
?>
回答by Mark Elliot
mysql_queryreturns a result handle, not the actual result. In other words, your query's result is saved as resource id #4 (and that's not guaranteed to be the same always, it's coincidence if you see it that way all the time).
mysql_query返回结果句柄,而不是实际结果。换句话说,您的查询结果将保存为资源 ID #4(并且不能保证始终相同,如果您一直这样看,那是巧合)。
To access the result you need to use something like mysql_fetch_arrayor one of the other functions in the same family.
要访问结果,您需要使用类似的功能mysql_fetch_array或同一系列中的其他功能之一。
Something like this:
像这样的东西:
<?php
$query = mysql_query("SELECT MAX(c_id) as max FROM customers");
$row = mysql_fetch_array($query);
$highest_id = $row['max'];
?>
回答by Amit Mhaske
This is my answer:
这是我的回答:
require_once 'db_cconnection.php';
$query = "SELECT MAX(stud_id) FROM student_tbl";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_row($result);
echo $row[0];
When you use mysqli_fetch_rowit will fetch only one row, as we only want one row. $rowwill be an array. So we need to get its value through array index.
当您使用mysqli_fetch_row它时,它只会获取一行,因为我们只需要一行。$row将是一个数组。所以我们需要通过数组索引来获取它的值。
回答by Shoaib Nawaz
$highest_id = mysql_result(mysql_query("SELECT MAX(c_id) FROM customers"));
回答by jsHate
the 2015 way of doing this
suppose you have database ($this->db)
2015 年的做法是
假设你有数据库 ($this->db)
$maxid = $this->db->query('SELECT MAX(c_id) FROM `customers`')->fetchColumn();

