使用MySQL查询浏览器,我手动创建了一个名为users
的表,并在字段中输入了一些日期。我将primary key
设置为id
并将其设置为auto increment
。有3 rows
,最高id
是3
。
然后我在class
目录中创建了以下method
来调用表格中的数据等。
class Application_Model_DbTable_User extends Zend_Db_Table_Abstract
{
protected $_name = 'user';
public function getLatestUserId()
{
$id = $this->getAdapter()->lastInsertId();
return $id;
}
}
在controller
我执行以下操作,获取method
生成的值并让view
访问它:
$usersDbModel = new Application_Model_DbTable_User();
$lastUserId = $usersDbModel->getLatestUserId();
$this->view->lastUserId = $lastUserId;
在视图中,我将其回显以显示给用户:
echo $this->lastUserId;
但是,即使我id
表中的上一个users
为3.它显示为0.
我也尝试过:
public function getLatestUserId()
{
$sql = 'SELECT max(id) FROM user';
$query = $this->query($sql);
$result = $query->fetchAll();
return $result;
}
但这只会引发服务器错误。
我做错了什么?
我错过了什么吗?
还有另一种方法吗?
答案 0 :(得分:2)
答案是:
$sql = 'SELECT max(id) FROM user';
$query = $this->getAdapter()->query($sql);
$result = $query->fetchAll();
return $result[0]['max(id)'];
答案 1 :(得分:1)
如果你查询过“SELECT id FROM USERS SORT BY id DESC LIMIT 0,1”
除了最新用户的ID,你什么也得不到?
答案 2 :(得分:0)
如果你使用像
这样的选择语句SELECT max(id) FROM USERS;
如果您还没有尝试在控制器中使用某个功能,请尝试这样的操作。
class IndexController extends Zend_Controller_Action {
public function init() {
}
public function indexAction() {
}
public function getLatestUserId()
{
$bootstrap = $this->getInvokeArg('bootstrap');
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();
$sqlrequest = "select max(id) as Idmax from user";
$rows = $db->fetchAll($sqlrequest);
foreach ($rows as $row)
echo $maxId = $row['Idmax'];
return $maxId;
}
}
并且您的引导程序文件看起来像
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initConfig()
{
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
return $config;
}
}
希望类似的东西有效,希望有所帮助
答案 3 :(得分:0)
问题是Mysql并不真正支持lastInsertId()
它在某些情况下会返回自动递增主键的最后一个id。
这里介绍了几种解决方案,大多数都会返回主键的最后一个ID,这可能是您真正需要的,也可能不是。
此方法也将使用select()对象执行相同操作。
public function getlastInsertId(){
$select = $this->select();
$select->from($this->_name, "id");
$select->order('id DESC');
$select->limit(0, 1);
$result = $this->fetchAll($select)->current();
return $result->id;
}
我认为随着时间的推移,如果你真正追求的是你插入的最后一个id,你会发现这些方法是不可靠的。随着时间的推移,当添加和删除记录时,数据库可能会填充或不填写空闲ID,具体取决于您当前使用的数据库以及该数据库的设置方式。
在大多数情况下,我们需要在插入后很快插入最后一项的id,通常是为了更新视图脚本。在这种情况下,我通常只将整个行对象作为返回的一部分。
这是我的意思的一个例子:
/**this method will save or update a record depending on data present in the array*/
public function saveUser(array $data) {
/**cast data array as std object for convience*/
$dataObject = (object) $data;
/**if id exists as array and has a non null value*/
if (array_key_exists('id', $data) && isset($data['id'])) {
/**find row if updating*/
$row = $this->find($dataObject->id)->current();
} else {
/**create new row*/
$row = $this->createRow();
}
$row->first_name = $dataObject->first_name;
$row->last_name = $dataObject->last_name;
$row->email = $dataObject->email;
/**save or update row*/
$row->save();
/**return the $row you just built or you can return $result = $row->save(); and get*/
/** just the primary key. Save() will throw an exception if table is marked read only*/
return $row;
}