【问题标题】:I'm using LAST_INSERT_ID() in php and mysql but i can't get a result. Why?我在 php 和 mysql 中使用 LAST_INSERT_ID() 但我无法得到结果。为什么?
【发布时间】:2013-05-25 15:36:52
【问题描述】:
$sql_comp5 ="INSERT INTO `tiquets` (`Id_Tiquet`) VALUES (NULL); SELECT LAST_INSERT_ID()";   
    $result8 = mysql_query($sql_comp5); 

     $flag_control=0;
    while ($row = mysql_fetch_assoc($result8, MYSQL_BOTH)) 
    {
        $flag_control=$flag_control+1;
             $id_t[$flag_control]=$row['LAST_INSERT_ID()'];              
    }


    for ($buc = 1; $buc <=$flag_control; $buc++)
    {
           $id_tiquet=$id_t[$buc];                
    }

我做对了吗?还是我错了?

非常感谢!!

【问题讨论】:

标签: php mysql last-insert-id


【解决方案1】:

您不能使用mysql_* 函数执行两个查询。为此,您需要mysqli::multi_query()。要使用 mysql_* 获取最后一个插入 ID,请使用 mysql_insert_id():

$sql_comp5 ="INSERT INTO `tiquets` (`Id_Tiquet`) VALUES (NULL);";
$result8 = mysql_query($sql_comp5); 
$id      = mysql_insert_id($result8);

【讨论】:

    【解决方案2】:

    删除第二个查询 - 并使用 php 的 mysql_insert_id()

    $sql_comp5 ="INSERT INTO tiquets (Id_Tiquet) 值 (NULL)";
    $result8 = mysql_query($sql_comp5); $insertedId = mysql_insert_id();

    【讨论】:

      猜你喜欢
      • 2016-05-17
      • 2020-04-29
      • 2013-08-07
      • 2023-03-03
      • 2018-02-25
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2013-01-25
      相关资源
      最近更新 更多