mysqli_connect.phpt 6.3 KB

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