mysqli_connect_oo.phpt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. --TEST--
  2. new mysqli()
  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. $obj = new stdClass();
  15. if ($mysqli = new mysqli($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket) && !mysqli_connect_errno())
  16. printf("[003] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
  17. $host, $user . 'unknown_really', $db, $port, $socket);
  18. if (false !== $mysqli)
  19. printf("[004] Expecting boolean/false, got %s/%s\n", gettype($mysqli), $mysqli);
  20. // Run the following tests without an anoynmous MySQL user and use a password for the test user!
  21. ini_set('mysqli.default_socket', $socket);
  22. if (!is_object($mysqli = new mysqli($host, $user, $passwd, $db, $port)) || (0 !== mysqli_connect_errno())) {
  23. printf("[005] Usage of mysqli.default_socket failed\n") ;
  24. } else {
  25. $mysqli->close();
  26. }
  27. ini_set('mysqli.default_port', $port);
  28. if (!is_object($mysqli = new mysqli($host, $user, $passwd, $db)) || (0 !== mysqli_connect_errno())) {
  29. printf("[006] Usage of mysqli.default_port failed\n") ;
  30. } else {
  31. $mysqli->close();
  32. }
  33. ini_set('mysqli.default_pw', $passwd);
  34. if (!is_object($mysqli = new mysqli($host, $user)) || (0 !== mysqli_connect_errno())) {
  35. printf("[007] Usage of mysqli.default_pw failed\n") ;
  36. } else {
  37. $mysqli->close();
  38. }
  39. ini_set('mysqli.default_user', $user);
  40. if (!is_object($mysqli = new mysqli($host)) || (0 !== mysqli_connect_errno())) {
  41. printf("[008] Usage of mysqli.default_user failed\n") ;
  42. } else {
  43. $mysqli->close();
  44. }
  45. ini_set('mysqli.default_host', $host);
  46. if (!is_object($mysqli = new mysqli()) || (0 !== mysqli_connect_errno())) {
  47. printf("[012] Failed to create mysqli object\n");
  48. } else {
  49. // There shall be NO connection! Using new mysqli(void) shall not use defaults for a connection!
  50. // We had long discussions on this and found that the ext/mysqli API as
  51. // such is broken. As we can't fix it, we document how it has behaved from
  52. // the first day on. And that's: no connection.
  53. if (NULL !== ($tmp = @$mysqli->query('SELECT 1'))) {
  54. printf("[013] There shall be no connection!\n");
  55. $mysqli->close();
  56. }
  57. }
  58. if ($IS_MYSQLND) {
  59. ini_set('mysqli.default_host', 'p:' . $host);
  60. if (!is_object($mysqli = new mysqli())) {
  61. // Due to an API flaw this shall not connect
  62. printf("[010] Failed to create mysqli object\n");
  63. } else {
  64. // There shall be NO connection! Using new mysqli(void) shall not use defaults for a connection!
  65. // We had long discussions on this and found that the ext/mysqli API as
  66. // such is broken. As we can't fix it, we document how it has behaved from
  67. // the first day on. And that's: no connection.
  68. if (NULL !== ($tmp = @$mysqli->query('SELECT 1'))) {
  69. printf("[011] There shall be no connection!\n");
  70. $mysqli->close();
  71. }
  72. }
  73. }
  74. print "... and now Exceptions\n";
  75. mysqli_report(MYSQLI_REPORT_OFF);
  76. mysqli_report(MYSQLI_REPORT_STRICT);
  77. try {
  78. $mysqli = new mysqli($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket);
  79. printf("[016] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
  80. $host, $user . 'unknown_really', $db, $port, $socket);
  81. $mysqli->close();
  82. } catch (mysqli_sql_exception $e) {
  83. printf("%s\n", $e->getMessage());
  84. }
  85. ini_set('mysqli.default_socket', $socket);
  86. try {
  87. $mysqli = new mysqli($host, $user, $passwd, $db, $port);
  88. $mysqli->close();
  89. } catch (mysqli_sql_exception $e) {
  90. printf("%s\n", $e->getMessage());
  91. printf("[017] Usage of mysqli.default_socket failed\n") ;
  92. }
  93. ini_set('mysqli.default_port', $port);
  94. try {
  95. $mysqli = new mysqli($host, $user, $passwd, $db);
  96. $mysqli->close();
  97. } catch (mysqli_sql_exception $e) {
  98. printf("%s\n", $e->getMessage());
  99. printf("[018] Usage of mysqli.default_port failed\n") ;
  100. }
  101. ini_set('mysqli.default_pw', $passwd);
  102. try {
  103. $mysqli = new mysqli($host, $user);
  104. $mysqli->close();
  105. } catch (mysqli_sql_exception $e) {
  106. printf("%s\n", $e->getMessage());
  107. printf("[019] Usage of mysqli.default_pw failed\n");
  108. }
  109. ini_set('mysqli.default_user', $user);
  110. try {
  111. $mysqli = new mysqli($host);
  112. $mysqli->close();
  113. } catch (mysqli_sql_exception $e) {
  114. printf("%s\n", $e->getMessage());
  115. printf("[020] Usage of mysqli.default_user failed\n") ;
  116. }
  117. ini_set('mysqli.default_host', $host);
  118. try {
  119. /* NOTE that at this point one must use a different syntax! */
  120. $mysqli = mysqli_init();
  121. $mysqli->real_connect();
  122. assert(0 === mysqli_connect_errno());
  123. $mysqli->close();
  124. assert(0 === mysqli_connect_errno());
  125. } catch (mysqli_sql_exception $e) {
  126. printf("%s\n", $e->getMessage());
  127. printf("[021] Usage of mysqli.default_host failed\n");
  128. }
  129. print "done!";
  130. ?>
  131. --EXPECTF--
  132. Warning: mysqli::mysqli(): (%s/%d): Access denied for user '%sunknown%s'@'%s' (using password: %s) in %s on line %d
  133. ... and now Exceptions
  134. Access denied for user '%s'@'%s' (using password: %s)
  135. done!