0

No matter what I try - I cant seem to pull the last created id of the query I inserted to mySql.

I read here about syntaxes that are deprecated and all sort of code that wont work.

I tried both functions (I use bigint) so I understand that this is how to go:

if (($result = $conn->query("SELECT LAST_INSERT_ID()")) === FALSE) {
  die(mysql_error()); 
}

if ($result->fetch_assoc()) {

$id = $row[0];
echo $id;
}

but nothing!!

Can someone please just give me a full simple php code sample of how to do it?

Bentoy13
  • 4,886
  • 1
  • 20
  • 33
user3495363
  • 349
  • 2
  • 11
  • http://stackoverflow.com/questions/10680943/pdo-get-the-last-id-inserted might help – Chris Lear Sep 30 '15 at 09:09
  • Don't use mysql_* it has been deprecated – randomizer Sep 30 '15 at 09:12
  • possible duplicate of [How do I get the last inserted ID of a MySQL table in PHP?](http://stackoverflow.com/questions/1685860/how-do-i-get-the-last-inserted-id-of-a-mysql-table-in-php) – revo Sep 30 '15 at 09:22
  • Nop, what I am asking for (and actually was just replied) it is nowhere to be found. – user3495363 Sep 30 '15 at 09:37
  • See, What you are asking is still unclear. Your code are totally wrong. Not getting from where to help. Format your question in meaningful way. OK – Nana Partykar Sep 30 '15 at 09:39

3 Answers3

2

You can use $result = $conn->insert_id; to know last inserted row

  • or any of the methods described in the manual http://php.net/manual/en/mysqli.insert-id.php – Strawberry Sep 30 '15 at 09:29
  • Thank you!!! and let me add that the $result you wrote might be confusing cause it is not an object. it returns a string. SO: echo $conn->insert_id; prints the last generated id. (Im using MyIsam, BigInt, Autoincrement index) – user3495363 Sep 30 '15 at 09:40
0

Try this to get the last insert Id,

echo mysql_insert_id();

user3040610
  • 750
  • 4
  • 15
0

Dont use mysql its depricated, instead use mysqli or PDO. To get last inserted record use mysqli_inserted_id(). The following shows the snipped how to use-

<?php
$link = mysqli_connect("localhost", "username", "password", "dbname") or die('Facing some problem connecting with database');

$query = "INSERT INTO `table_name` VALUES (v1, v2, v3...vn)";
mysqli_query($link, $query);

printf ("New Record with id %d inserted", mysqli_insert_id($link));
Channaveer Hakari
  • 2,769
  • 3
  • 34
  • 45