mysqli_affected_rows.phpt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. --TEST--
  2. mysqli_affected_rows()
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. require_once('skipifconnectfailure.inc');
  8. ?>
  9. --FILE--
  10. <?php
  11. require_once("connect.inc");
  12. if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  13. printf("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  14. $host, $user, $db, $port, $socket);
  15. if (0 !== ($tmp = mysqli_affected_rows($link)))
  16. printf("[005] Expecting int/0, got %s/%s. [%d] %s\n",
  17. gettype($tmp), $tmp, mysqli_errno($link), mysqli_error($link));
  18. if (!mysqli_query($link, 'DROP TABLE IF EXISTS test'))
  19. printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  20. if (!mysqli_query($link, 'CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine))
  21. printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  22. if (!mysqli_query($link, "INSERT INTO test(id, label) VALUES (1, 'a')"))
  23. printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  24. if (1 !== ($tmp = mysqli_affected_rows($link)))
  25. printf("[010] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
  26. // ignore INSERT error, NOTE: command line returns 0, affected_rows returns -1 as documented
  27. mysqli_query($link, "INSERT INTO test(id, label) VALUES (1, 'a')");
  28. if (-1 !== ($tmp = mysqli_affected_rows($link)))
  29. printf("[011] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
  30. if (!mysqli_query($link, "INSERT INTO test(id, label) VALUES (1, 'a') ON DUPLICATE KEY UPDATE id = 4"))
  31. printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  32. if (2 !== ($tmp = mysqli_affected_rows($link)))
  33. printf("[013] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
  34. if (!mysqli_query($link, "INSERT INTO test(id, label) VALUES (2, 'b'), (3, 'c')"))
  35. printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  36. if (2 !== ($tmp = mysqli_affected_rows($link)))
  37. printf("[015] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
  38. if (!mysqli_query($link, "INSERT IGNORE INTO test(id, label) VALUES (1, 'a')")) {
  39. printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  40. }
  41. if (1 !== ($tmp = mysqli_affected_rows($link)))
  42. printf("[017] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
  43. if (!mysqli_query($link, "INSERT INTO test(id, label) SELECT id + 10, label FROM test"))
  44. printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  45. if (4 !== ($tmp = mysqli_affected_rows($link)))
  46. printf("[019] Expecting int/4, got %s/%s\n", gettype($tmp), $tmp);
  47. if (!mysqli_query($link, "REPLACE INTO test(id, label) values (4, 'd')"))
  48. printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  49. if (2 !== ($tmp = mysqli_affected_rows($link)))
  50. printf("[021] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
  51. if (!mysqli_query($link, "REPLACE INTO test(id, label) values (5, 'e')"))
  52. printf("[022] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  53. if (1 !== ($tmp = mysqli_affected_rows($link)))
  54. printf("[023] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
  55. if (!mysqli_query($link, "UPDATE test SET label = 'a' WHERE id = 2"))
  56. printf("[024] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  57. if (1 !== ($tmp = mysqli_affected_rows($link)))
  58. printf("[025] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
  59. $charsets = array('utf8');
  60. foreach ($charsets as $k => $charset) {
  61. if (!($res = mysqli_query($link, sprintf("SHOW CHARACTER SET LIKE '%s'", $charset))))
  62. continue;
  63. mysqli_free_result($res);
  64. if (true !== ($tmp = mysqli_set_charset($link, $charset)))
  65. printf("[026] Expecting boolean/true got %s/%s\n",
  66. gettype($tmp), $tmp);
  67. if (0 !== ($tmp = mysqli_affected_rows($link)))
  68. printf("[027] Expecting int/0 got %s/%s\n", gettype($tmp), $tmp);
  69. }
  70. if (!mysqli_query($link, "UPDATE test SET label = 'a' WHERE id = 2")) {
  71. printf("[028] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  72. }
  73. if (0 !== ($tmp = mysqli_affected_rows($link)))
  74. printf("[029] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  75. if (!mysqli_query($link, "UPDATE test SET label = 'a' WHERE id = 100")) {
  76. printf("[030] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  77. }
  78. if (0 !== ($tmp = mysqli_affected_rows($link)))
  79. printf("[031] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  80. if (!mysqli_query($link, 'DROP TABLE IF EXISTS test'))
  81. printf("[032] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  82. mysqli_close($link);
  83. try {
  84. mysqli_affected_rows($link);
  85. } catch (Error $exception) {
  86. echo $exception->getMessage() . "\n";
  87. }
  88. print "done!";
  89. ?>
  90. --CLEAN--
  91. <?php
  92. require_once("clean_table.inc");
  93. ?>
  94. --EXPECT--
  95. mysqli object is already closed
  96. done!