简体   繁体   中英

I want to fetch last inserted data from database

I created table like this

id
a1
a2
a3
a
a4
a5
a6

1
3
1
7
63
3
5
0


2
3
1
7
35
3
5
0

3
3
1
7
40
3
5
0


5
1
5
5
44
2
2
2

6
5
6
9
07
5
5
7

7
5
6
9
07
5
5
7

Now I want to fetch latest data from this table to homepage of my website.

Latest row would be max id.

On home page it should show like this:

a1a2a3 - a - a4a5a6

Means

On home page it should show {- is separator} like this:

I used following code but it prints only a value which is INR of 2

<?php

mysql_connect("localhost","username","password");
mysql_select_db(dbname);

$q = "SELECT a FROM table
ORDER BY id DESC
LIMIT 1;";
$result = mysql_query($q);
$data = mysql_fetch_array($result);

echo $data[0];

?>

Why do you select only a single column a ? try CONCAT(col1,col2,...)

$q = "SELECT CONCAT(a1,a2,a3,' - ',a,' - ',a4,a5,a6) FROM result ORDER BY id DESC
LIMIT 1;";

You can use the function mysql_insert_id() to get the last auto incrementing Id. http://php.net/manual/en/function.mysql-insert-id.php

$query = "Select max (id) as theid from table";
$result = Mysqli_query ($link, $query);
$r = mysqli_fetch_object ($result);
Echo $r->theid;

Should do it. The mysqli_insert_id function will only work if the most recent query was an insert query which doesn't appear is the case in the question.

Hope that helps.

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