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    $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    $tmp = @mysqli_connect($link);
23    if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
24        printf("[002] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
25    }
26
27    $tmp = @mysqli_connect($link, $link);
28    if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
29        printf("[003] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
30    }
31
32    $tmp = @mysqli_connect($link, $link, $link);
33    if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
34        printf("[004] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
35    }
36
37    $tmp = @mysqli_connect($link, $link, $link, $link);
38    if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
39        printf("[005] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
40    }
41
42    $tmp = @mysqli_connect($link, $link, $link, $link, $link);
43    if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
44        printf("[006] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
45    }
46
47    $tmp = @mysqli_connect($link, $link, $link, $link, $link, $link);
48    if (($anon_allow && gettype($tmp) != "object") || (!$anon_allow && $tmp != false)) {
49        printf("[007] Expecting %s, got %s/%s\n", $exptype, gettype($tmp), $tmp);
50    }
51
52    if (!$link = mysqli_connect($host, $user, $passwd, $db, $port, $socket))
53        printf("[008] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
54            $host, $user, $db, $port, $socket);
55
56    mysqli_close($link);
57
58    if ($link = mysqli_connect($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket))
59        printf("[009] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
60            $host, $user . 'unknown_really', $db, $port, $socket);
61
62    if (false !== $link)
63        printf("[010] Expecting boolean/false, got %s/%s\n", gettype($link), $link);
64
65    // Run the following tests without an anoynmous MySQL user and use a password for the test user!
66    ini_set('mysqli.default_socket', $socket);
67    if (!is_object($link = mysqli_connect($host, $user, $passwd, $db, $port))) {
68        printf("[011] Usage of mysqli.default_socket failed\n") ;
69    } else {
70        if (!$res = mysqli_query($link, "SELECT 'mysqli.default_socket' AS 'testing'"))
71            printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
72        var_dump(mysqli_fetch_assoc($res));
73        mysqli_free_result($res);
74        mysqli_close($link);
75    }
76
77    ini_set('mysqli.default_port', $port);
78    if (!is_object($link = mysqli_connect($host, $user, $passwd, $db))) {
79        printf("[013] Usage of mysqli.default_port failed\n") ;
80    } else {
81        if (!$res = mysqli_query($link, "SELECT 'mysqli.default_port' AS 'testing'"))
82            printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
83        var_dump(mysqli_fetch_assoc($res));
84        mysqli_free_result($res);
85        mysqli_close($link);
86    }
87
88    ini_set('mysqli.default_pw', $passwd);
89    if (!is_object($link = mysqli_connect($host, $user))) {
90        printf("[015] Usage of mysqli.default_pw failed\n") ;
91    } else {
92        if (!$res = mysqli_query($link, "SELECT 'mysqli.default_pw' AS 'testing'"))
93            printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
94        var_dump(mysqli_fetch_assoc($res));
95        mysqli_free_result($res);
96        mysqli_close($link);
97    }
98
99    ini_set('mysqli.default_user', $user);
100    if (!is_object($link = mysqli_connect($host))) {
101        printf("[017] Usage of mysqli.default_user failed\n") ;
102    } else {
103        if (!$res = mysqli_query($link, "SELECT 'mysqli.default_user' AS 'testing'"))
104            printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
105        var_dump(mysqli_fetch_array($res, MYSQLI_BOTH));
106        mysqli_free_result($res);
107        mysqli_close($link);
108    }
109
110    ini_set('mysqli.default_host', $host);
111    if (!is_object($link = mysqli_connect())) {
112        printf("[019] Usage of mysqli.default_host failed\n") ;
113    } else {
114        if (!$res = mysqli_query($link, "SELECT 'mysqli.default_host' AS 'testing'"))
115            printf("[020] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
116        var_dump(mysqli_fetch_array($res, MYSQLI_NUM));
117        mysqli_free_result($res);
118        mysqli_close($link);
119    }
120
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    print "done!";
144?>
145--EXPECTF--
146Warning: mysqli_connect(): (%s/%d): Access denied for user '%s'@'%s'%r( \(using password: \w+\)){0,1}%r in %s on line %d
147array(1) {
148  ["testing"]=>
149  string(21) "mysqli.default_socket"
150}
151array(1) {
152  ["testing"]=>
153  string(19) "mysqli.default_port"
154}
155array(1) {
156  ["testing"]=>
157  string(17) "mysqli.default_pw"
158}
159array(2) {
160  [0]=>
161  string(19) "mysqli.default_user"
162  ["testing"]=>
163  string(19) "mysqli.default_user"
164}
165array(1) {
166  [0]=>
167  string(19) "mysqli.default_host"
168}
169done!
170