0

I am trying to get last auto incremented customer ID from customer table using mysql_insert_ID function and store it in a variable named lastid and try to send lastid variable using session on another page and printing it there it won't give any output or error

  • 3
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde May 09 '17 at 17:23

3 Answers3

0
//---page1.php
// Start a session to be able to store SESSION variables.
session_start();

// Your connection to Database
$conn = new mysqli("localhost", "my_user", "my_password", "world");

// Your insert query
$insert_query = "INSERT INTO customers .....";

// Query the database
$conn->query( $insert_query );

// STORE Last_inserted_id into $lastid and $_SESSION
$_SESSION['lastid'] = $lastid = $conn->insert_id;


//---page2.php
// Start a session to be able to use SESSION variables in the new page.
session_start();

// Get value from $_SESSION
echo $_SESSION['lastid'];

Taken from documentation $insert_id and session_start()

Mike Casan Ballester
  • 1,690
  • 19
  • 33
-2

PDO is the one way to do this if you want to do with PDO then here you can follow this:

After a successful query simply use lastInsertId method with your PDO instance :

$lastId = $pdo->lastInsertId(); //This variable will store last insert ID

For More details about lastInsertId() Method you may check this official Documentation PHP Last Insert Id

If you think that will be better to use mysqli for you Then

Just simply after a successful query do like this:

$lastId = $mysqli->insert_id; // this is how you will get last inserted id

You may follow this stackoverflow question: Follow This

Community
  • 1
  • 1
Code Cooker
  • 881
  • 14
  • 19
-2

Since you're familiar with mysql, use mysqli... in that case:

$sql = "SELECT max(id) as max FROM table";

From there:

$result = $conn->query($sql);
$row = $result->fetch_assoc();
$maxID = $row['max']

If you wish to stay in mysql_

$sql = "SELECT max(id) as max FROM table";

From there:

$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$maxID = $row['max']
clearshot66
  • 2,292
  • 1
  • 8
  • 17
  • This isn't the way to do this mysqli have built-in property `$mysqli->insert_id` – Code Cooker May 09 '17 at 17:39
  • It's one method of it, so yes, it is a way to do it. You also have to use insert_id right after an insert. He doesn't state that's what's happening. Max(id) is surefire to get the highest ID (the latest in an auto increment situation) versus insert_id. You should do some research before answering people's questions and giving false advise. – clearshot66 May 09 '17 at 17:49
  • This can Mis-match sometime and not appropriate. – Code Cooker May 09 '17 at 17:49
  • The efficient way is to do use of `insert_id` don't just try to re invent the wheel – Code Cooker May 09 '17 at 17:50
  • _Coding for our nations higher-education facilities._ Frightening! On a busy database this would most likely get a completely different id and really foul things up – RiggsFolly May 10 '17 at 18:38
  • Someone's full of themselves quoting bio's. – clearshot66 May 10 '17 at 18:44