mysqli_connect_oo.phpt 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. --TEST--
  2. new mysqli()
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  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. try {
  54. $mysqli->query('SELECT 1');
  55. } catch (Error $exception) {
  56. echo $exception->getMessage() . "\n";
  57. }
  58. }
  59. if ($IS_MYSQLND) {
  60. ini_set('mysqli.default_host', 'p:' . $host);
  61. if (!is_object($mysqli = new mysqli())) {
  62. // Due to an API flaw this shall not connect
  63. printf("[010] Failed to create mysqli object\n");
  64. } else {
  65. // There shall be NO connection! Using new mysqli(void) shall not use defaults for a connection!
  66. // We had long discussions on this and found that the ext/mysqli API as
  67. // such is broken. As we can't fix it, we document how it has behaved from
  68. // the first day on. And that's: no connection.
  69. try {
  70. $mysqli->query('SELECT 1');
  71. } catch (Error $exception) {
  72. echo $exception->getMessage() . "\n";
  73. }
  74. }
  75. }
  76. print "... and now Exceptions\n";
  77. mysqli_report(MYSQLI_REPORT_OFF);
  78. mysqli_report(MYSQLI_REPORT_STRICT);
  79. try {
  80. $mysqli = new mysqli($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket);
  81. printf("[016] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
  82. $host, $user . 'unknown_really', $db, $port, $socket);
  83. $mysqli->close();
  84. } catch (mysqli_sql_exception $e) {
  85. printf("%s\n", $e->getMessage());
  86. }
  87. ini_set('mysqli.default_socket', $socket);
  88. try {
  89. $mysqli = new mysqli($host, $user, $passwd, $db, $port);
  90. $mysqli->close();
  91. } catch (mysqli_sql_exception $e) {
  92. printf("%s\n", $e->getMessage());
  93. printf("[017] Usage of mysqli.default_socket failed\n") ;
  94. }
  95. ini_set('mysqli.default_port', $port);
  96. try {
  97. $mysqli = new mysqli($host, $user, $passwd, $db);
  98. $mysqli->close();
  99. } catch (mysqli_sql_exception $e) {
  100. printf("%s\n", $e->getMessage());
  101. printf("[018] Usage of mysqli.default_port failed\n") ;
  102. }
  103. ini_set('mysqli.default_pw', $passwd);
  104. try {
  105. $mysqli = new mysqli($host, $user);
  106. $mysqli->close();
  107. } catch (mysqli_sql_exception $e) {
  108. printf("%s\n", $e->getMessage());
  109. printf("[019] Usage of mysqli.default_pw failed\n");
  110. }
  111. ini_set('mysqli.default_user', $user);
  112. try {
  113. $mysqli = new mysqli($host);
  114. $mysqli->close();
  115. } catch (mysqli_sql_exception $e) {
  116. printf("%s\n", $e->getMessage());
  117. printf("[020] Usage of mysqli.default_user failed\n") ;
  118. }
  119. ini_set('mysqli.default_host', $host);
  120. try {
  121. /* NOTE that at this point one must use a different syntax! */
  122. $mysqli = mysqli_init();
  123. $mysqli->real_connect();
  124. assert(0 === mysqli_connect_errno());
  125. $mysqli->close();
  126. assert(0 === mysqli_connect_errno());
  127. } catch (mysqli_sql_exception $e) {
  128. printf("%s\n", $e->getMessage());
  129. printf("[021] Usage of mysqli.default_host failed\n");
  130. }
  131. print "done!";
  132. ?>
  133. --EXPECTF--
  134. Warning: mysqli::__construct(): (%s/%d): Access denied for user '%sunknown%s'@'%s' (using password: %s) in %s on line %d
  135. mysqli object is not fully initialized
  136. mysqli object is not fully initialized
  137. ... and now Exceptions
  138. Access denied for user '%s'@'%s' (using password: %s)
  139. done!