开发者

php/sql need help with query to get last title

I have a form where you enter something and it then shows you the id and title of last row, but as its being created there and then it just 开发者_运维问答shows the previous last row at time of submission, ie if last row id is 23 it would show row 23 when I actually want it to show row 24, to get around this I added +1 to id in the query below:

ie:

$last_id = $lr_result['id'] + 1;

now this works fine for the id but now I'm trying to get the title of the same row but whatever I try I always get the last title (at time of submission), ie row 23 title rather then row 24 title (or nothing at all in the case for the snippet below).

// fetch id
$lastrow    = "SELECT * FROM foo ORDER BY id DESC LIMIT 1";
$lr_result  = mysql_fetch_assoc(mysql_query($lastrow));
//fetch id and add 1
$last_id    = $lr_result['id'] + 1;

// fetch title where id = last_id
$lasttitlerow = "SELECT * FROM foo WHERE id='{$last_id}'";
$ltr_result   = mysql_fetch_assoc(mysql_query($lasttitlerow));
$last_title   = $ltr_result['title'];

echo $last_id . $last_title;

As always all help is appreciated and thanks in advance.


SELECT * FROM foo WHERE id = (SELECT max(id) FROM foo)

perhaps? The inner query would return '23', and the outerquery will fetch the entire record. You wouldn't be able to retrieve id=24, because that record doesn't exist yet - you haven't inserted it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜