mysql_affected_rows.phpt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. --TEST--
  2. mysql_affected_rows()
  3. --SKIPIF--
  4. <?php
  5. require_once('skipif.inc');
  6. require_once('skipifconnectfailure.inc');
  7. ?>
  8. --FILE--
  9. <?php
  10. include_once("connect.inc");
  11. include_once('setupdefault.inc');
  12. $tmp = NULL;
  13. $link = NULL;
  14. if (0 !== ($tmp = @mysql_affected_rows()))
  15. printf("[001] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  16. if (null !== ($tmp = @mysql_affected_rows($link)))
  17. printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  18. if (!is_null($tmp = @mysql_affected_rows($link, $link)))
  19. printf("[003] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
  20. if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket))
  21. printf("[004] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  22. $host, $user, $db, $port, $socket);
  23. if (-1 !== ($tmp = mysql_affected_rows($link)))
  24. printf("[005] Expecting int/-1, got %s/%s. [%d] %s\n",
  25. gettype($tmp), $tmp, mysql_errno($link), mysql_error($link));
  26. if (!mysql_query('DROP TABLE IF EXISTS test', $link))
  27. printf("[006] [%d] %s\n", mysql_errno($link), mysql_error($link));
  28. if (!mysql_query('CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine, $link))
  29. printf("[007] [%d] %s\n", mysql_errno($link), mysql_error($link));
  30. if (!mysql_query("INSERT INTO test(id, label) VALUES (1, 'a')", $link))
  31. printf("[008] [%d] %s\n", mysql_errno($link), mysql_error($link));
  32. if (1 !== ($tmp = mysql_affected_rows($link)))
  33. printf("[010] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
  34. // ignore INSERT error, NOTE: command line returns 0, affected_rows returns -1 as documented
  35. @mysql_query("INSERT INTO test(id, label) VALUES (1, 'a')", $link);
  36. if (-1 !== ($tmp = mysql_affected_rows($link)))
  37. printf("[011] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
  38. if (!mysql_query("INSERT INTO test(id, label) VALUES (1, 'a') ON DUPLICATE KEY UPDATE id = 4", $link))
  39. printf("[012] [%d] %s\n", mysql_errno($link), mysql_error($link));
  40. if (2 !== ($tmp = mysql_affected_rows($link)))
  41. printf("[013] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
  42. if (!mysql_query("INSERT INTO test(id, label) VALUES (2, 'b'), (3, 'c')", $link))
  43. printf("[014] [%d] %s\n", mysql_errno($link), mysql_error($link));
  44. if (2 !== ($tmp = mysql_affected_rows($link)))
  45. printf("[015] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
  46. if (!mysql_query("INSERT IGNORE INTO test(id, label) VALUES (1, 'a')", $link)) {
  47. printf("[016] [%d] %s\n", mysql_errno($link), mysql_error($link));
  48. }
  49. if (1 !== ($tmp = mysql_affected_rows($link)))
  50. printf("[017] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
  51. if (!mysql_query("INSERT INTO test(id, label) SELECT id + 10, label FROM test", $link))
  52. printf("[018] [%d] %s\n", mysql_errno($link), mysql_error($link));
  53. if (4 !== ($tmp = mysql_affected_rows($link)))
  54. printf("[019] Expecting int/4, got %s/%s\n", gettype($tmp), $tmp);
  55. if (!mysql_query("REPLACE INTO test(id, label) values (4, 'd')", $link))
  56. printf("[020] [%d] %s\n", mysql_errno($link), mysql_error($link));
  57. if (2 !== ($tmp = mysql_affected_rows($link)))
  58. printf("[021] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
  59. if (!mysql_query("REPLACE INTO test(id, label) values (5, 'e')", $link))
  60. printf("[022] [%d] %s\n", mysql_errno($link), mysql_error($link));
  61. if (1 !== ($tmp = mysql_affected_rows($link)))
  62. printf("[023] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
  63. if (!mysql_query("UPDATE test SET label = 'a' WHERE id = 2", $link))
  64. printf("[024] [%d] %s\n", mysql_errno($link), mysql_error($link));
  65. if (1 !== ($tmp = mysql_affected_rows($link)))
  66. printf("[025] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
  67. if (!mysql_query("UPDATE test SET label = 'a' WHERE id = 2", $link)) {
  68. printf("[025] [%d] %s\n", mysql_errno($link), mysql_error($link));
  69. }
  70. if (0 !== ($tmp = mysql_affected_rows($link)))
  71. printf("[026] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  72. if (!mysql_query("UPDATE test SET label = 'a' WHERE id = 100", $link)) {
  73. printf("[025] [%d] %s\n", mysql_errno($link), mysql_error($link));
  74. }
  75. if (0 !== ($tmp = mysql_affected_rows($link)))
  76. printf("[026] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  77. if (0 !== ($tmp = mysql_affected_rows()))
  78. printf("[027] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
  79. if (!mysql_query('DROP TABLE IF EXISTS test', $link))
  80. printf("[028] [%d] %s\n", mysql_errno($link), mysql_error($link));
  81. mysql_close($link);
  82. if (false !== ($tmp = @mysql_affected_rows($link)))
  83. printf("[029] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
  84. print "done!";
  85. ?>
  86. --CLEAN--
  87. <?php
  88. require_once("clean_table.inc");
  89. ?>
  90. --EXPECTF--
  91. 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
  92. done!