访问PHP/MySQL中最后创建的行 [英] Accessing last created row in PHP/MySQL

查看:53
本文介绍了访问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()?

推荐答案

如果"id"列是自动递增的,则可以使用

If you 'id' column is an auto-increment, you can use mysql_insert_id :

检索为 前一个AUTO_INCREMENTINSERT查询.

Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆