A
A
Alexander Zatogin2015-09-24 15:14:06
PHP
Alexander Zatogin, 2015-09-24 15:14:06

How to select last id from sql table?

I need to select the last assigned id from the database when loading the page. Ids are assigned automatically. In phpmyadmin, the code works, but it does not display data on the page (Id must be displayed not on the registration page). Here is my code:

$sql = "SELECT LAST_INSERTED_ID FROM members";
  while ($row = mysql_fetch_assoc($sql)) {
    echo "<span class='num'>" + $row['id'] + "<span>";
  }

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alexander, 2015-09-24
@Anekseria

SELECT MAX(`id`) FROM `members`

O
Optimus, 2015-09-24
Pyan @marrk2

"SELECT id FROM table ORDER BY id DESC LIMIT 1"

A
Alexey Bespalov, 2015-09-24
@nulldef

PHP has a function like mysql_insert_id . I think this is what you need.
PS I beg you, do not use outdated database functions. On kraynyak - mysqli extension. And ideally - PDO

A
Alexander Zatogin, 2015-09-24
@Anekseria

Nothing happens. The page loads without errors, but the result is not displayed anywhere.

A
Alex Safonov, 2015-09-24
@elevenelven

$sql = "SELECT LAST_INSERTED_ID FROM members";
$sql = mysql_query ($sql);
  while ($row = mysql_fetch_assoc($sql)) {
    echo "<span class='num'>" . $row['id'] . "<span>";
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question