mysqli_connect.phpt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. --TEST--
  2. mysqli_connect()
  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. /* we need to check, if the server allows anonymous login (empty user) */
  15. $tmp = @mysqli_connect('localhost');
  16. $anon_allow = (gettype($tmp) == "object");
  17. $exptype = ($anon_allow) ? "mysqli_object" : "false";
  18. $obj = new stdClass();
  19. if (!is_null($tmp = @mysqli_connect($obj)))
  20. printf("[001] Expecting NULL got %s/%s\n", gettype($tmp), $tmp);
  21. $tmp = @mysqli_connect($link);
  22. if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
  23. printf("[002] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
  24. }
  25. $tmp = @mysqli_connect($link, $link);
  26. if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
  27. printf("[003] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
  28. }
  29. $tmp = @mysqli_connect($link, $link, $link);
  30. if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
  31. printf("[004] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
  32. }
  33. $tmp = @mysqli_connect($link, $link, $link, $link);
  34. if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
  35. printf("[005] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
  36. }
  37. $tmp = @mysqli_connect($link, $link, $link, $link, $link);
  38. if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
  39. printf("[006] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
  40. }
  41. $tmp = @mysqli_connect($link, $link, $link, $link, $link, $link);
  42. if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
  43. printf("[007] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
  44. }
  45. if (!$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket))
  46. printf("[008] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  47. $host, $user, $db, $port, $socket);
  48. mysqli_close($link);
  49. if ($link = mysqli_connect($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket))
  50. printf("[009] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
  51. $host, $user . 'unknown_really', $db, $port, $socket);
  52. if (false !== $link)
  53. printf("[010] Expecting boolean/false, got %s/%s\n", gettype($link), $link);
  54. // Run the following tests without an anoynmous MySQL user and use a password for the test user!
  55. ini_set('mysqli.default_socket', $socket);
  56. if (!is_object($link = mysqli_connect($host, $user, $passwd, $db, $port))) {
  57. printf("[011] Usage of mysqli.default_socket failed\n") ;
  58. } else {
  59. if (!$res = mysqli_query($link, "SELECT 'mysqli.default_socket' AS 'testing'"))
  60. printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  61. var_dump(mysqli_fetch_assoc($res));
  62. mysqli_free_result($res);
  63. mysqli_close($link);
  64. }
  65. ini_set('mysqli.default_port', $port);
  66. if (!is_object($link = mysqli_connect($host, $user, $passwd, $db))) {
  67. printf("[013] Usage of mysqli.default_port failed\n") ;
  68. } else {
  69. if (!$res = mysqli_query($link, "SELECT 'mysqli.default_port' AS 'testing'"))
  70. printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  71. var_dump(mysqli_fetch_assoc($res));
  72. mysqli_free_result($res);
  73. mysqli_close($link);
  74. }
  75. ini_set('mysqli.default_pw', $passwd);
  76. if (!is_object($link = mysqli_connect($host, $user))) {
  77. printf("[015] Usage of mysqli.default_pw failed\n") ;
  78. } else {
  79. if (!$res = mysqli_query($link, "SELECT 'mysqli.default_pw' AS 'testing'"))
  80. printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  81. var_dump(mysqli_fetch_assoc($res));
  82. mysqli_free_result($res);
  83. mysqli_close($link);
  84. }
  85. ini_set('mysqli.default_user', $user);
  86. if (!is_object($link = mysqli_connect($host))) {
  87. printf("[017] Usage of mysqli.default_user failed\n") ;
  88. } else {
  89. if (!$res = mysqli_query($link, "SELECT 'mysqli.default_user' AS 'testing'"))
  90. printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  91. var_dump(mysqli_fetch_array($res, MYSQLI_BOTH));
  92. mysqli_free_result($res);
  93. mysqli_close($link);
  94. }
  95. ini_set('mysqli.default_host', $host);
  96. if (!is_object($link = mysqli_connect())) {
  97. printf("[019] Usage of mysqli.default_host failed\n") ;
  98. } else {
  99. if (!$res = mysqli_query($link, "SELECT 'mysqli.default_host' AS 'testing'"))
  100. printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  101. var_dump(mysqli_fetch_array($res, MYSQLI_NUM));
  102. mysqli_free_result($res);
  103. mysqli_close($link);
  104. }
  105. if ($IS_MYSQLND) {
  106. ini_set('mysqli.default_host', 'p:' . $host);
  107. if (!is_object($link = mysqli_connect())) {
  108. printf("[021] Usage of mysqli.default_host (persistent) failed\n") ;
  109. } else {
  110. if (!$res = mysqli_query($link, "SELECT 'mysqli.default_host (persistent)' AS 'testing'"))
  111. printf("[022] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
  112. $tmp = mysqli_fetch_assoc($res);
  113. if ($tmp['testing'] !== 'mysqli.default_host (persistent)') {
  114. printf("[023] Result looks strange - check manually, [%d] %s\n",
  115. mysqli_errno($link), mysqli_error($link));
  116. var_dump($tmp);
  117. }
  118. mysqli_free_result($res);
  119. mysqli_close($link);
  120. }
  121. ini_set('mysqli.default_host', 'p:');
  122. if (is_object($link = @mysqli_connect())) {
  123. printf("[024] Usage of mysqli.default_host=p: did not fail\n") ;
  124. mysqli_close($link);
  125. }
  126. }
  127. print "done!";
  128. ?>
  129. --EXPECTF--
  130. Warning: mysqli_connect(): (%s/%d): Access denied for user '%s'@'%s' (using password: YES) in %s on line %d
  131. array(1) {
  132. [%u|b%"testing"]=>
  133. %unicode|string%(21) "mysqli.default_socket"
  134. }
  135. array(1) {
  136. [%u|b%"testing"]=>
  137. %unicode|string%(19) "mysqli.default_port"
  138. }
  139. array(1) {
  140. [%u|b%"testing"]=>
  141. %unicode|string%(17) "mysqli.default_pw"
  142. }
  143. array(2) {
  144. [0]=>
  145. %unicode|string%(19) "mysqli.default_user"
  146. [%u|b%"testing"]=>
  147. %unicode|string%(19) "mysqli.default_user"
  148. }
  149. array(1) {
  150. [0]=>
  151. %unicode|string%(19) "mysqli.default_host"
  152. }
  153. done!