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