简体   繁体   中英

how to show last inserted record id from mysql database on live site?

I want to show highest record id from mysql database on live. I am using following code and it's working on localhost but not on live site.

 <?php
$q ="SELECT LAST_INSERT_ID()";
$result = mysqli_query($q);
$data = mysqli_fetch_array($result);
echo $data[0];
?> 

you can use id column if you are using as PK and auto increment.

Select * from TABLE order BY id DECS LIMIT 1

if you are talking about concurrent queries then i have done something different to tackle this situation.

insert a timestamp in a new column and then fetch the same please check the below code

$token= data();

insert into TABLE ('val1', 'val2', $token);

and then

you can user $token to get id of last inserted row by something like this

Select * from TABLE where token = $token

As Jon mentioned you need to add MAX(). So did you setup your live database exactly the same as the localhost one? Maybe tell us the error? (Can't comment yet, sorry)

use sub select query if id is unique.

select row from table where id=( select max(id) from table )

if id is not unique then use this.

select row from table ORDER BY id DESC LIMIT 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM