简体   繁体   English

MySQL返回MIN ID而不是MAX ID?

[英]MySQL returning the MIN id instead of the MAX id?

I have a table with an AI column called 'id'. 我有一个带有名为“ id”的AI列的表。 This column is UNSIGNED and has 6 entries. 该列是未签名的,具有6个条目。 So id: 6 is the MAX value in the column. 因此id:6是该列中的MAX值。 Using PHP to call forth the max id in the table, it always prints out the least value. 使用PHP调用表中的最大id,它总是打印出最小值。 This is what i'm doing: 这就是我在做什么:

$MAX_ID = $db->query("SELECT MAX(id) FROM table");
                    echo "Hello" + $MAX_ID;

I've tried all the ways of doing this, like ORDER BY and id=("SELECT FROM MAX(id)"), but 1 is still being returned. 我已经尝试过所有方法,例如ORDER BY和id =(“ SELECT FROM MAX(id)”),但仍返回1。 I am using PHPMyAdmin, and when I do the SQL query there, the right value is being returned. 我正在使用PHPMyAdmin,并且在那里执行SQL查询时,将返回正确的值。 What am I doing wrong? 我究竟做错了什么?

No, what you're getting is just a MySQLi result object ( $MAX_ID ). 不,您得到的只是一个MySQLi结果对象( $MAX_ID )。

Execute the query → Fetch the rows. 执行查询→提取行。

$query = $db->query("SELECT MAX(id) FROM Entertainment"); // execute
$max_id = $query->fetch_array(); // fetch
echo $max_id[0];

Additional Note: Use . 附加说明:使用. for concatenation, not + : 用于连接,而不是+

echo 'max: ' . $max_id[0];

Take a look at the mysqli documentation . 看一下mysqli文档 The value being returned is a result object, not the direct result data from the query: You'll need to use mysqli's accessor methods to extract actual result rows. 返回的值是结果对象,而不是查询的直接结果数据:您将需要使用mysqli的访问器方法来提取实际结果行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM