开发者

how to get last inserted id - zend

I'm trying to get latest inserted id from a table using this code:

$id = $tbl->fetchAll (array('public=1'), 'id desc');

but it's always returning "1"

any ideas?


update: I've just discovered toArray();, which retrieves all the data from fetchAll. The problem is, I only need the ID. My current code looks like this:

$rowsetArray = $id->toArray();
$rowCount = 1;

foreach ($rowsetArray as $rowArray) {
    foreach ($rowArray as $column => $value) {
   开发者_StackOverflow社区    if ($column="id") {$myid[$brr] = $value;}
      //echo"\n$myid[$brr]";
    }
    ++$rowCount;
    ++$brr;
}

Obviously, I've got the if ($column="id") {$myid[$brr] = $value;} thing wrong. Can anyone point me in the right direction?

An aternative would be to filter ID's from fetchAll. Is that possible?


Think you can use:

$id = $tbl->lastInsertId();


Aren't you trying to get last INSERT id from SELECT query?

Use lastInsertId() or the value returned by insert: $id = $db->insert();


Why are you using fetchAll() to retrieve the last inserted ID? fetchAll() will return a rowset of results (multiple records) as an object (not an array, but can be converted into an array using the toArray() method). However, if you are trying to reuse a rowset you already have, and you know the last record is the first record in the rowset, you can do this:

$select = $table->select()
    ->where('public = 1')
    ->order('id DESC');
$rows = $table->fetchAll($select);
$firstRow = $rows->current();
$lastId = $firstRow->id;

If you were to use fetchRow(), it would return a single row, so you wouldn't have to call current() on the result:

$select = $table->select()
    ->where('public = 1')
    ->order('id DESC');
$row = $table->fetchRow($select);
$lastId = $row->id;


It sounds like it's returning true rather than the actual value. Check the return value for the function fetchAll

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜