I'm building a magazine management system. I've made archive for magazines using sql db table and php. I want to show the latest magazine data on the home page by getting the last auto incremented id from the archive table from db. But how? I'm Using SQL, not PDO
Asked
Active
Viewed 1,113 times
0
-
are you using MYSQLI or PDO? – Rotimi Apr 25 '17 at 07:26
-
1You tried `MAX(id)`? – Sebastian Brosch Apr 25 '17 at 07:26
-
It would help if you could show some code. – Rotimi Apr 25 '17 at 07:26
-
What is the problem? To find the row or display the magazine cover? – jarlh Apr 25 '17 at 07:26
-
`$last_id = $conn->insert_id;` to get last insert id – webpic Apr 25 '17 at 07:28
-
I'm using Procedural sql – tawsif torabi Apr 25 '17 at 07:31
4 Answers
3
The easiest way to get what you want is probably the following:
SELECT *
FROM magazines
ORDER BY id DESC
LIMIT 1
But you might want to consider creating a date entered column to use instead of the auto increment ID.

Tim Biegeleisen
- 502,043
- 27
- 286
- 360
-
-
I've wrote this code after you answered, it is showing this error, **Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in** – tawsif torabi Apr 25 '17 at 07:36
-
-
Should I have double quotes? I've tried, it shows a syntax error – tawsif torabi Apr 25 '17 at 07:42
-
_Don't_ put quotes around the table name. Backticks if you must, but not quotes. – Tim Biegeleisen Apr 25 '17 at 07:43
-
@tawsiftorabi - pls stop using `mysql_*`: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Sebastian Brosch Apr 25 '17 at 08:04
-
1
order by is easy way and if you only 1 record then use limit
select * from magazines order by id desc limit 1;
you can use mysql max function
select max(id) as ID from magazines;
and if you want to get last insert id in insert query then use this
$last_id = $conn->insert_id; // mysqli
$last_id = mysql_insert_id(); // mysql but remove from php 7
$lastId = $dbh->lastInsertId(); // pdo

Shafiqul Islam
- 5,570
- 2
- 34
- 43
0
If you're using PDO, use PDO::lastInsertId.
If you're using Mysqli, use mysqli::$insert_id.
If you're still using Mysql:mysql_insert_id.

Ravi Kumar
- 190
- 2
- 12
0
The Query should be
SELECT max(ID) from magazine
This will return the maximum and the most recent id. You can then pass this query to desired PHP API

Maaz Rehman
- 674
- 1
- 7
- 20