douban2014 2019-07-04 12:31
浏览 88
已采纳

无法从html中的sql检索最后插入的id

i am trying to display some values from database to my html calling the last inserted id

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * from registers where id= LAST_INSERT_ID()";
$result = $conn->query($sql);

if (!empty($result) && $result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo " $row[firstname] ";
    }
} else {
    echo "0 results";
}

But when I am using this, the result is not being displayed, is there any problem with my code, how do I retrieve the last inserted id from sql?

</div>
  • 写回答

2条回答 默认 最新

  • doubengshao8872 2019-07-04 12:56
    关注

    try this query

    $sql = "SELECT * from registers ORDER BY ID DESC limit 1 ";
    

    it will give you only one result and this result will be the last inserted ID. But ID needs to be auto increment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?