How to use insert_id()

454 views
Skip to first unread message

Rafael Martinez

unread,
Feb 3, 2014, 7:14:05 PM2/3/14
to not...@googlegroups.com

I want to get the last inserted id!

I try..

if ($insertadoMedia = $db->media()->insert(array('url' => $videoId, 'tipo' => $tipoMedia))) {
$ultimoId=$db->media()->insert_id(); // not working  
                        $ultimoId= $insertadoMedia->insert_id(); // not working

echo $ultimoId;
} else {
echo "hubo un error al insertar";
}

please help me.... example!

Hari K T

unread,
Feb 3, 2014, 8:18:43 PM2/3/14
to not...@googlegroups.com

Hari K T

You can ring me : +91 9388 75 8821

Skype  : kthari85
Twitter : harikt


--
You received this message because you are subscribed to the Google Groups "NotORM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to notorm+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Message has been deleted

Rafael Martinez

unread,
Feb 4, 2014, 8:48:02 AM2/4/14
to not...@googlegroups.com
I can persist with the normal database, but I have two tables one called MEDIA and the other is called POST, POST has a relationship with MEDIA, taking POST the ID MEDIA, then I need to insert first MEDIA to get the ID and thus be able to INSERT INTO POST with the MEDIA id. that's why I need to know how to use INSERT_ID ()

Jakub Vrana

unread,
Feb 4, 2014, 12:12:25 PM2/4/14
to not...@googlegroups.com
This should work:

$media = $db->media()->insert(...);
$media->post()->insert(...);

See this example:
https://github.com/vrana/notorm/blob/master/tests/10-update.phpt

Jakub

Toni Tabak

unread,
Oct 30, 2014, 5:41:04 PM10/30/14
to not...@googlegroups.com
Hi i was working on this for last 6 hours, row that is returned after insert dont have id.
I insert in mysql table with id auto increment, and $clients have only data that i send in $array, not inserted AI id ?

$clients = $db->clients()->insert($array);

$clients["id"] is empty string.

I can do:
echo $db->clients()->insert_id();
to get last inserted id but if 2 users insert something in same time one user can get id from second insertion.
I don't understend how someone else dont have same problem, i search and this is only post on google about this quesrtion.

Allysson David

unread,
Oct 30, 2014, 6:12:30 PM10/30/14
to not...@googlegroups.com
No, it's working just fine for me.
Details of my testing at the end.

Note:
The 'insert' function fills the 'id' column with (but there are conditions):
$id = $this->notORM->connection->lastInsertId($this->notORM->structure->getSequence($this->table))

The 'insert_id' function returns:
$this->notORM->connection->lastInsertId()

So it all goes to the PDO, since it's where the 'lastInsertId' function is defined.
And it may not be consistent across all database drivers - here.

Testing:
I used a MySQL database to test the following code:
$array = array('value1' => 1, 'value2' => 2);
$clientes = $db->clients()->insert($array);
echo $clientes['id'];

The database structure was as follows:
CREATE TABLE `clients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value1` int(11) NOT NULL,
`value2` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

Refreshing the page always successfully showed the ID. 

Jakub Vrana

unread,
Oct 31, 2014, 12:29:35 PM10/31/14
to not...@googlegroups.com
Hi,

lastInsertId is per connection, you don't have to worry that some other
user gets your ID. The code for setting the ID column is at
https://github.com/vrana/notorm/blob/e49d5d2f1bfe440dc82b61f46172635dfcb6f6dd/NotORM/Result.php#L259
You can add some debugging there to see what's going on.

Jakub
> > an email to notorm+un...@googlegroups.com <javascript:>.
> > For more options, visit https://groups.google.com/groups/opt_out
> <https://groups.google.com/groups/opt_out>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "NotORM" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to notorm+un...@googlegroups.com
> <mailto:notorm+un...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages