mysqli_insert_id.phpt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. --TEST--
  2. mysqli_insert_id()
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifemb.inc');
  7. require_once('skipifconnectfailure.inc');
  8. ?>
  9. --FILE--
  10. <?php
  11. require_once("connect.inc");
  12. $tmp = NULL;
  13. $link = NULL;
  14. if (!is_null($tmp = @mysqli_insert_id()))
  15. printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  16. if (!is_null($tmp = @mysqli_insert_id($link)))
  17. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  18. require('table.inc');
  19. if (0 !== ($tmp = mysqli_insert_id($link)))
  20. printf("[003] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  21. if (!$res = mysqli_query($link, "SELECT id, label FROM test ORDER BY id LIMIT 1")) {
  22. printf("[004] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  23. }
  24. if (0 !== ($tmp = mysqli_insert_id($link)))
  25. printf("[005] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  26. mysqli_free_result($res);
  27. // no auto_increment column
  28. if (!$res = mysqli_query($link, "INSERT INTO test(id, label) VALUES (100, 'a')")) {
  29. printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  30. }
  31. if (0 !== ($tmp = mysqli_insert_id($link)))
  32. printf("[007] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  33. if (!$res = mysqli_query($link, "ALTER TABLE test MODIFY id INT NOT NULL AUTO_INCREMENT")) {
  34. printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  35. }
  36. if (!$res = mysqli_query($link, "INSERT INTO test(label) VALUES ('a')")) {
  37. printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  38. }
  39. if (($last_id = mysqli_insert_id($link)) <= 0)
  40. printf("[010] Expecting int/any >0, got %s/%s\n", gettype($last_id), $last_id);
  41. if (mysqli_query($link, "LOCK TABLE test WRITE")) {
  42. /* we need exclusive access for a moment */
  43. /* let's hope nobody changes auto_increment_increment while this code executes */
  44. do {
  45. if (mysqli_get_server_version($link) >= 50000) {
  46. if (!$res = mysqli_query($link, 'SELECT @@auto_increment_increment AS inc')) {
  47. printf("[011] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  48. break;
  49. }
  50. if (!$row = mysqli_fetch_assoc($res)) {
  51. printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  52. break;
  53. }
  54. mysqli_free_result($res);
  55. $inc = $row['inc'];
  56. } else {
  57. $inc = 1;
  58. }
  59. if (!mysqli_query($link, "INSERT INTO test(label) VALUES ('b')")) {
  60. printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  61. break;
  62. }
  63. if (($next_id = mysqli_insert_id($link)) <= $last_id)
  64. /*
  65. very likely a bug, but someone could have done something on the server
  66. between the second last insert and the lock, therefore don't stop just bail
  67. */
  68. printf("[014] Expecting int/any > %d, got %s/%s\n", $last_id, gettype($next_id), $next_id);
  69. $last_id = $next_id;
  70. if (!mysqli_query($link, "INSERT INTO test(label) VALUES ('c'), ('d'), ('e')")) {
  71. printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  72. break;
  73. }
  74. /*
  75. Note: For a multiple-row insert, LAST_INSERT_ID() and mysql_insert_id() actually
  76. return the AUTO_INCREMENT key from the first of the inserted rows. This allows
  77. multiple-row inserts to be reproduced correctly on other servers in a replication setup.
  78. */
  79. if (($next_id = mysqli_insert_id($link)) != $last_id + $inc) {
  80. printf("[016] Expecting int/%d, got %s/%s\n", $last_id + 1, gettype($next_id), $next_id);
  81. break;
  82. }
  83. if (!$res = mysqli_query($link, "SELECT LAST_INSERT_ID() AS last_id")) {
  84. printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  85. break;
  86. }
  87. if (!$row = mysqli_fetch_assoc($res)) {
  88. printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  89. break;
  90. }
  91. mysqli_free_result($res);
  92. if ($next_id != $row['last_id']) {
  93. printf("[019] Something is wrong, check manually. Expecting %s got %s.\n",
  94. $next_id, $row['last_id']);
  95. break;
  96. }
  97. } while (false);
  98. mysqli_query($link, "UNLOCK TABLE test");
  99. }
  100. if (!$res = mysqli_query($link, "INSERT INTO test(id, label) VALUES (1000, 'a')")) {
  101. printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  102. }
  103. if (1000 !== ($tmp = mysqli_insert_id($link)))
  104. printf("[021] Expecting int/1000, got %s/%s\n", gettype($tmp), $tmp);
  105. if (!$res = mysqli_query($link, "INSERT INTO test(label) VALUES ('b'), ('c')")) {
  106. printf("[022] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  107. }
  108. if (1000 >= ($tmp = mysqli_insert_id($link)))
  109. printf("[023] Expecting int/>1000, got %s/%s\n", gettype($tmp), $tmp);
  110. mysqli_close($link);
  111. var_dump(mysqli_insert_id($link));
  112. print "done!";
  113. ?>
  114. --CLEAN--
  115. <?php
  116. require_once("clean_table.inc");
  117. ?>
  118. --EXPECTF--
  119. Warning: mysqli_insert_id(): Couldn't fetch mysqli in %s on line %d
  120. NULL
  121. done!