Guest User

Untitled

a guest
Apr 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. mysql_query('INSERT INTO FOO(a) VALUES('b')');
  2. $id = mysql_insert_id();
  3.  
  4. $pdo->lastInsertId();
  5.  
  6. $mysqli->insert_id;
  7.  
  8. $id = mysql_insert_id();
  9.  
  10. $id = last_insert_id();
  11.  
  12. $this->db->insert('mytable',$data);
  13. echo $this->db->insert_id(); //returns last inserted id
  14.  
  15. echo $this->db->insert('mytable',$data)->insert_id();
  16.  
  17. $link = mysqli_connect("localhost", "my_user", "my_password", "world");
  18.  
  19. $query = "INSERT blah blah blah...";
  20. $result = mysqli_query($link, $query);
  21.  
  22. echo mysqli_insert_id($link);
  23.  
  24. SELECT AUTO_INCREMENT FROM information_schema.TABLES
  25. WHERE TABLE_SCHEMA = 'my_database'
  26. AND TABLE_NAME = 'my_table_name';
  27.  
  28. $sql="INSERT INTO table (col1, col2, col3) VALUES (val1, val2, val3)";
  29. $mysqli->query($sql);
  30. $last_inserted_id=$mysqli->insert_id; // returns last ID
  31.  
  32. $sql="INSERT INTO table (col1, col2, col3) VALUES (val1, val2, val3)";
  33. $database->query($sql);
  34. $last_inserted_id=$database->lastInsertId(); // returns last ID
  35.  
  36. $selectquery="SELECT id FROM tableName ORDER BY id DESC LIMIT 1";
  37. $result = $mysqli->query($selectquery);
  38. $row = $result->fetch_assoc();
  39. echo $row['id'];
  40.  
  41. <?php
  42.  
  43. function getInsertId(mysqli &$instance, $enforceQuery = false){
  44. if(!$enforceQuery)return $instance->insert_id;
  45.  
  46. $result = $instance->query('SELECT LAST_INSERT_ID();');
  47.  
  48. if($instance->errno)return false;
  49.  
  50. list($buffer) = $result->fetch_row();
  51.  
  52. $result->free();
  53.  
  54. unset($result);
  55.  
  56. return $buffer;
  57. }
  58.  
  59. ?>
  60.  
  61. <?php
  62. $mysqli = new mysqli("localhost", "yourUsername", "yourPassword", "yourDB");
  63.  
  64. /* check connection */
  65. if (mysqli_connect_errno()) {
  66. printf("Connect failed: %sn", mysqli_connect_error());
  67. exit();
  68. }
  69. // Conside employee table with id,name,designation
  70. $query = "INSERT INTO myCity VALUES (NULL, 'Ram', 'Developer')";
  71. $mysqli->query($query);
  72.  
  73. printf ("New Record has id %d.n", $mysqli->insert_id);
  74.  
  75. /* close connection */
  76. $mysqli->close();
  77. ?>
  78.  
  79. $stmt = $db->prepare("...");
  80. $stmt->execute();
  81. $id = $db->lastInsertId();
  82.  
  83. $con = mysqli_connect('localhost', 'userid', 'password', 'database_name');
  84. if (!$con) {
  85. die('Could not connect: ' . mysqli_error($con));
  86. }
  87. $sql = "SELECT * FROM table_name where UserID=(select MAX(UserID)from table_name)";
  88. $result = mysqli_query($con, $sql);
  89.  
  90. mysql_query("INSERT INTO mytable (product) values ('kossu')");
  91.  
  92. printf("Last inserted record has id %dn", ***mysql_insert_id()***);
Add Comment
Please, Sign In to add comment