php MySql如何获取行数据的最大值(id)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14004295/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:25:24  来源:igfitidea点击:

How to get max(id) of row data MySql

phpmysqlcodeigniter

提问by kiriappa

I want to get the maximum id of row data. In my table first column is id then firstname and etc. this is the sql command I used to get the max(id) of row data.

我想获得行数据的最大 id。在我的表中,第一列是 id 然后是名字等等。这是我用来获取行数据的 max(id) 的 sql 命令。

<?PHP  $maxid=$this->db->query("SELECT MAX(id) FROM `emplyee_personal_details`");
                print_r( $maxid) ;?>

but it prints bunch of data as it is a array. But I need only the maximam id of row data for validation before data inserting and updating.

但它打印了一堆数据,因为它是一个数组。但在数据插入和更新之前,我只需要行数据的最大值 id 进行验证。

How to get the maxid. I use codeigniter framework.

如何获得maxid。我使用 codeigniter 框架。

回答by Shomz

Try this:

尝试这个:

$maxid = $this->db->query('SELECT MAX(id) AS `maxid` FROM `emplyee_personal_details`')->row()->maxid;

UPDATE

更新

This will work even if your table is empty (unlike my example above):

即使您的表是空的(与我上面的示例不同),这也将起作用:

$maxid = 0;
$row = $this->db->query('SELECT MAX(id) AS `maxid` FROM `emplyee_personal_details`')->row();
if ($row) {
    $maxid = $row->maxid; 
}

回答by Derek Nutile

The problem with using a raw query like "SELECT MAX(id) ..." is it is not abstract and may not work for every SQL engine. I think the active record way to do it is like this:

使用像 "SELECT MAX(id) ..." 这样的原始查询的问题是它不是抽象的,可能不适用于每个 SQL 引擎。我认为活动记录的方式是这样的:

$this->db->select_max('id');
$query = $this->db->get('emplyee_personal_details');
// Produces: SELECT MAX(id) as age FROM emplyee_personal_details

See: http://ellislab.com/codeigniter/user-guide/database/active_record.html

请参阅:http: //ellislab.com/codeigniter/user-guide/database/active_record.html

回答by Raham

Try this,hope it helps,

试试这个,希望有帮助

return $this->db->select_max('id')
                        ->get('your_table_name')
                        ->row()->id;

回答by Ray Paseur

SELECT id FROM table ORDER BY id DESC LIMIT 1

That will get you the highest id value, and when I read this, "it prints bunch of data as it is a array" I get the sense that what you really want is a part of that array. DB queries always return complex structures like arrays or objects. So if you wanted just the scalar value (the number as an integer) you might use something like this:

这将使您获得最高的 id 值,当我阅读本文时,“它会打印一堆数据,因为它是一个数组”,我感觉到您真正想要的是该数组的一部分。DB 查询总是返回复杂的结构,如数组或对象。因此,如果您只想要标量值(整数形式的数字),您可以使用以下内容:

$maxid = (int)$maxid['id'];

or like this (if you have an object):

或者像这样(如果你有一个对象):

$maxid = (int)$maxid->id;

HTH, ~Ray

HTH,~雷

回答by Hamdan Shafiq

public function getMaxCategoryId() {
    $query = $this->db->query("SELECT category_id+1 AS maxid FROM " . DB_PREFIX . "category ORDER BY category_id DESC LIMIT 1");
    return $query->row['maxid'];
}

error undefined index maxid

错误未定义索引 maxid

回答by Abdullah Ayoub

<?php`$qry = "select max(ID)+1 As ID from records";`
$result = $con->query($qry);
$row = $result->fetch_assoc();`echo "New ID To Enter = ".$row["ID"];?>

After Connection Just Write This Code It Will Work

连接后只需编写此代码即可工作