mysql_insert_id.phpt 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. --TEST--
  2. mysql_insert_id()
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifconnectfailure.inc');
  7. ?>
  8. --FILE--
  9. <?php
  10. include "connect.inc";
  11. include 'setupdefault.inc';
  12. $tmp = NULL;
  13. $link = NULL;
  14. if (0 !== ($tmp = @mysql_insert_id()))
  15. printf("[001] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  16. if (NULL !== ($tmp = @mysql_insert_id($link)))
  17. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  18. if (!is_null($tmp = @mysql_insert_id($link, 'too many args')))
  19. printf("[002a] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  20. require('table.inc');
  21. if (0 !== ($tmp = mysql_insert_id($link)))
  22. printf("[003] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  23. if (!$res = mysql_query("SELECT id, label FROM test ORDER BY id LIMIT 1", $link)) {
  24. printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link));
  25. }
  26. if (0 !== ($tmp = mysql_insert_id($link)))
  27. printf("[005] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  28. mysql_free_result($res);
  29. // no auto_increment column
  30. if (!$res = mysql_query("INSERT INTO test(id, label) VALUES (100, 'a')", $link)) {
  31. printf("[006] [%d] %s\n", mysql_errno($link), mysql_error($link));
  32. }
  33. if (0 !== ($tmp = mysql_insert_id($link)))
  34. printf("[007] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  35. if (!$res = mysql_query("ALTER TABLE test MODIFY id INT NOT NULL AUTO_INCREMENT", $link)) {
  36. printf("[008] [%d] %s\n", mysql_errno($link), mysql_error($link));
  37. }
  38. if (!$res = mysql_query("INSERT INTO test(label) VALUES ('a')", $link)) {
  39. printf("[009] [%d] %s\n", mysql_errno($link), mysql_error($link));
  40. }
  41. if (0 === ($tmp = mysql_insert_id($link)))
  42. printf("[010] Expecting int/not zero, got %s/%s\n", gettype($tmp), $tmp);
  43. if ($tmp !== ($tmp2 = mysql_insert_id()))
  44. printf("[011] Expecting %s/%s, got %s/%s\n",
  45. gettype($tmp), $tmp,
  46. gettype($tmp2), $tmp2);
  47. mysql_close($link);
  48. var_dump(mysql_insert_id($link));
  49. print "done!";
  50. ?>
  51. --CLEAN--
  52. <?php
  53. require_once("clean_table.inc");
  54. ?>
  55. --EXPECTF--
  56. Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in %s on line %d
  57. Warning: mysql_insert_id(): %d is not a valid MySQL-Link resource in %s on line %d
  58. bool(false)
  59. done!