访问PHP/MySQL中最后创建的行 [英] Accessing last created row in PHP/MySQL
本文介绍了访问PHP/MySQL中最后创建的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用PHP/MySQL访问刚插入数据库的行?
How to access the row which has just been inserted into a DB with PHP/MySQL?
我有:
$sql = 'INSERT INTO `jos_db`.`jos_sections` (`id`, `name`) VALUES (NULL, \'foo\')';
mysql_query($sql, $dbi);
bar();
如何访问bar()中的新行?
How do I access the new row in bar()?
推荐答案
If you 'id' column is an auto-increment, you can use mysql_insert_id :
检索为 前一个
AUTO_INCREMENT
列INSERT
查询.
Retrieves the ID generated for an
AUTO_INCREMENT
column by the previousINSERT
query.
手册中给出的示例如下所示:
The example given in the manual looks like this :
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
这篇关于访问PHP/MySQL中最后创建的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文