如何知道刚刚插入数据库那条数据的id

一、总结

一句话总结:这些常见功能各个框架里面都有,可以查看手册,thinkphp里面是$userId = Db::name('user')->getLastInsID();

1、在mysql和mysqli中如何选择?

用mysqli,php官方推荐

2、mysqli中如何查找到刚刚出入数据库的那条数据的id?

mysqli对象的indert_id属性

$mysqli->insert_id

二、PHP如何找到刚刚插入数据库的一条数据的ID?

$_title=$_POST['input_title'];
$_described=$_POST['described'];
mysql_query("insert into questionnaire (quesTitle,quesDescribe,createTime) values('$_title','$_described',now())");
header(index.php?quesid=刚刚插入的那个编号)

如上所述,我刚刚插入的一条数据,现在要立刻跳到该数据自动生成的id的页面去,怎么获取呢??

你看看 mysql_insert_id 这个函数. 获取上一步insert 插入成功的id, 不成功的时候是没有值的

mysql_insert_id() 函数返回上一步 INSERT 操作产生的 ID。 没事多自己百度一下,多看手册,别一有问题就到处发帖,这种只要一分钟就能知道的答案 ,你却要花十几分钟,甚至几个小时,甚至没人回答,我只能说一句,哥们 你这是何苦呢?

mysql_insert_id()将 MySQL 内部的 C API 函数 mysql_insert_id()的返回值转换成 long(PHP 中命名为 int)。如果 AUTO_INCREMENT 的列的类型是 BIGINT,则 mysql_insert_id()返回的值将不正确。可以在 SQL 查询中用 MySQL 内部的 SQL 函数 LAST_INSERT_ID()来替代。
例子一:]mysql_insert_id()]例子b]b]
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
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());
?>
 
function mysql_insert_id ($link_identifier = null) {}

/**
* @deprecated 5.5
* Get result data
* @link http://php.net/manual/en/function.mysql-result.php
* @param resource $result
* @param int $row <p>
* The row number from the result that's being retrieved. Row numbers
* start at 0.
* </p>
* @param mixed $field [optional] <p>
* The name or offset of the field being retrieved.
* </p>
* <p>
* It can be the field's offset, the field's name, or the field's table
* dot field name (tablename.fieldname). If the column name has been
* aliased ('select foo as bar from...'), use the alias instead of the
* column name. If undefined, the first field is retrieved.
* </p>
* @return string The contents of one cell from a MySQL result set on success, or
* false on failure.
* @since 4.0
* @since 5.0
*/

$mysqli->insert_id

 Example #1 $mysqli->insert_id example

 面向对象风格

 <?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} $mysqli->query("CREATE TABLE myCity LIKE City"); $query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query); printf ("New Record has id %d.\n", $mysqli->insert_id); /* drop table */
$mysqli->query("DROP TABLE myCity"); /* close connection */
$mysqli->close();
?>
thinkphp 5.0
多去查看参考手册,里面肯定有

insert 方法添加数据成功返回添加成功的条数,insert 正常情况返回 1

添加数据后如果需要返回新增数据的自增主键,可以使用getLastInsID方法:

Db::name('user')->insert($data);
$userId = Db::name('user')->getLastInsID();

或者直接使用insertGetId方法新增数据并返回主键值:

Db::name('user')->insertGetId($data);

insertGetId 方法添加数据成功返回添加数据的自增主键

 
 
 
 

如何知道刚刚插入数据库那条数据的id的更多相关文章

  1. mybatis 做 insert操作的时候返回插入的那条数据的id

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:吃丸子的小鹿链接:http://www.zhihu.com/question/20810321/answer/16843223 ...

  2. 绝对干货,教你4分钟插入1000万条数据到mysql数据库表,快快进来

    我用到的数据库为,mysql数据库5.7版本的 1.首先自己准备好数据库表 其实我在插入1000万条数据的时候遇到了一些问题,现在先来解决他们,一开始我插入100万条数据时候报错,控制台的信息如下: ...

  3. 插入1000万条数据到mysql数据库表

    转自:https://www.cnblogs.com/fanwencong/p/5765136.html 我用到的数据库为,mysql数据库5.7版本的 1.首先自己准备好数据库表 其实我在插入100 ...

  4. [MyBatis]五分钟向MySql数据库插入一千万条数据 批量插入 用时5分左右

    本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar 我的数据库环境是mys ...

  5. 一张自增表里面总共有 7 条数据,删除了最后 2 条数据,重启 MySQL 数据库,又插入了一条数据,此时 id 是几?如何获取当前数据库版本?

    一张自增表里面总共有 7 条数据,删除了最后 2 条数据,重启 MySQL 数据库,又插入了一条数据,此时 id 是几? 一般情况下,我们创建的表的类型是InnoDB,如果新增一条记录(不重启mysq ...

  6. 使用hibernate在5秒内插入11万条数据,你觉得可能吗?

    需求是这样的,需要查询某几个表的数据,然后插入到另外一个表. 一看到需求,很多人都会用hibernate去把这些数据都查询出来,然后放到list中, 然后再用for循环之类的进行遍历,一条一条的取出数 ...

  7. Jmeter连接mysql,如何用delete、update、insert真正删除、更改、插入数据库里的数据;

    1.如下图,当插入数据的时候如图对应填写,查询数据的时候上面插入的那条数据就会显示,但是如果不执行下图的提交数据:到数据库里查的时候,插入的这条数据实际上并没有插入成功: . 结果:如果没有提交数据, ...

  8. 关于如何在mysql中插入一条数据后,返回这条数据的id

    简单的总结一下如何在mysql中出入一条数据后,返回该条数据的id ,假如之后代码需要这个id,这样做起来就变得非常方便,内容如下: <insert id="insertAndGetI ...

  9. 教你如何6秒钟往MySQL插入100万条数据!然后删库跑路!

    教你如何6秒钟往MySQL插入100万条数据!然后删库跑路! 由于我用的mysql 8版本,所以增加了Timezone,然后就可以了 前提是要自己建好库和表. 数据库test, 表user, 三个字段 ...

随机推荐

  1. 平衡树之RB-tree

    #include <memory> template<class T> struct rb_node { T key; bool color;//true red | fals ...

  2. [Angular & Unit Testing] Testing a RouterOutlet component

    The way to test router componet needs a little bit setup, first we need to create a "router-stu ...

  3. 怎样在nat方式的虚拟机下做ssh连接

    很多人在本机做測试都是用桥接的方式让虚拟机上网. 假设ip地址紧张或者根本就不同意我们拥有一个局域网的ip.这时候便能够使用NAT方式+putty来远程操作. 第一步,打开设备-Network-更改网 ...

  4. Catch Me If You ... Can't Do Otherwise--转载

    原文地址:https://dzone.com/articles/catch-me-if-you-cant-do-otherwise I don't know whether it's an anti- ...

  5. 【Good Bye 2017 C】 New Year and Curling

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举前i-1个圆. 哪些圆和它相交. 取圆心纵坐标最大的那个圆就可以了. [代码] #include <bits/stdc++ ...

  6. 邮件协议与port

          电子邮箱的协议有SMTP.POP2.POP3.IMAP4等.都隶属于TCP/IP协议簇,默认状态下.分别通过TCPport25.110和143建立连接.针对不同的用途和功能,我们在邮件se ...

  7. Apple Watch 集成环信SDK

    本文简单的讲述下怎样用Apple Watch Kit集成环信SDK. 升级xcode到version 6.2,和 IOS SDK8.2 下载环信SDK从官网 打开XCode->new proje ...

  8. ubuntu-系统卡慢解决(转载)

    ubuntu系统狠慢 或者很卡的原因 1.  涉及内存小或者虚拟SWAP分区调整问题    可以通过 系统监视器 进行查看      在UBUNTU系统里面,并不是你的物理内存全部耗尽之后,系统才使用 ...

  9. JAVA 水题

    纯粹是让我来掌握熟练度的. 1.金蝉素数 某古寺的一块石碑上依稀刻有一些神秘的自然数. 专家研究发现:这些数是由1,3,5,7,9 这5 个奇数字排列组成的5 位素数,且同时去掉它的最高位与最低位数字 ...

  10. 使用Invoke解决多线程间的控件访问出错

    // 按钮点击事件处理程序private void button1_Click(object sender, EventArgs e){    //创建新线程    Thread processorT ...