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