zoukankan      html  css  js  c++  java
  • 如何知道刚刚插入数据库那条数据的id

    如何知道刚刚插入数据库那条数据的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
    ", 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

     1 Example #1 $mysqli->insert_id example
     2 
     3 面向对象风格
     4 
     5 <?php
     6 $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
     7 
     8 /* check connection */
     9 if (mysqli_connect_errno()) {
    10     printf("Connect failed: %s
    ", mysqli_connect_error());
    11     exit();
    12 }
    13 
    14 $mysqli->query("CREATE TABLE myCity LIKE City");
    15 
    16 $query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
    17 $mysqli->query($query);
    18 
    19 printf ("New Record has id %d.
    ", $mysqli->insert_id);
    20 
    21 /* drop table */
    22 $mysqli->query("DROP TABLE myCity");
    23 
    24 /* close connection */
    25 $mysqli->close();
    26 ?>
    thinkphp 5.0
    多去查看参考手册,里面肯定有

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

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

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

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

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

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

     
     
     
     
  • 相关阅读:
    Sql Server 2008卸载后再次安装一直报错
    listbox 报错 Cannot have multiple items selected when the SelectionMode is Single.
    Sql Server 2008修改Sa密码
    学习正则表达式
    Sql Server 查询第30条数据到第40条记录数
    Sql Server 复制表
    Sql 常见面试题
    Sql Server 简单查询 异步服务器更新语句
    jQuery stop()用法以及案例展示
    CSS3打造不断旋转的CD封面
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/9254593.html
Copyright © 2011-2022 走看看