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