0

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];
?> 
MilanPanchal
  • 2,943
  • 1
  • 19
  • 37
Neeya
  • 135
  • 8

3 Answers3

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

Sudik Maharana
  • 704
  • 1
  • 7
  • 16
  • i am not getting please mentioned it in my query – Neeya Apr 04 '17 at 10:46
  • I had a situation to where to find last inserted id after insertion. so I used this method. as mahesh mentioned, if you have concurrency issue then you need to add a new column "token" in your table and then while inserting, insert the local timestamp to that column. After that use that timestamp value to fetch the newly added row. – Sudik Maharana Apr 04 '17 at 10:49
0

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)

Oliver
  • 151
  • 10
  • If you can't comment, the best thing to do is to provide a real answer, rather than asking clarity on the answer box, so that you can earn enough rep to comment – Masivuye Cokile Apr 04 '17 at 10:36
  • my concern only is there i want to print last last inserted id on UI. i am using above code, it working on local but not live please suggest – Neeya Apr 04 '17 at 10:47
  • Yes we will try it. But first of all what's your problem? Please describe your error-code or whatever. If your code is not working, then it is something about your database or the PHP-Compiler on the live System. @Neeya – Oliver Apr 04 '17 at 10:50
  • @OliverBähler there is no any error showing on wesite. it showing blank. same code working on localhost but on live site it is not working. i want to print last inserted id on frontend of my website. thats clear. is there any other suggestions so please help – Neeya Apr 04 '17 at 11:18
  • As I said: 1 - Make sure the PHP-Interpreter is the same live as local (7.0.0 vs. lower). 2 - Is the Database structur the same on the live database?. - 3 Do you have access to the live database? - 4. Has the user you're connecting via. php enough permissions to access the database? - 5. Is your database-Server running correctly? @Neeya – Oliver Apr 04 '17 at 11:40
0

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

Keyur Bhanderi
  • 1,524
  • 1
  • 11
  • 17