1--TEST--
2new mysqli()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once 'skipifconnectfailure.inc';
8?>
9--FILE--
10<?php
11    require_once 'connect.inc';
12
13    if ($mysqli = new mysqli($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket) && !mysqli_connect_errno())
14        printf("[003] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
15            $host, $user . 'unknown_really', $db, $port, $socket);
16
17    if (false !== $mysqli)
18        printf("[004] Expecting boolean/false, got %s/%s\n", gettype($mysqli), $mysqli);
19
20    // Run the following tests without an anoynmous MySQL user and use a password for the test user!
21    ini_set('mysqli.default_socket', $socket);
22    $mysqli = new mysqli($host, $user, $passwd, $db, $port);
23    if (0 !== mysqli_connect_errno()) {
24        printf("[005] Usage of mysqli.default_socket failed\n") ;
25    } else {
26        $mysqli->close();
27    }
28
29    ini_set('mysqli.default_port', $port);
30    $mysqli = new mysqli($host, $user, $passwd, $db);
31    if (0 !== mysqli_connect_errno()) {
32        printf("[006] Usage of mysqli.default_port failed\n") ;
33    } else {
34        $mysqli->close();
35    }
36
37    ini_set('mysqli.default_pw', $passwd);
38    $mysqli = new mysqli($host, $user);
39    if (0 !== mysqli_connect_errno()) {
40        printf("[007] Usage of mysqli.default_pw failed\n") ;
41    } else {
42        $mysqli->close();
43    }
44
45    ini_set('mysqli.default_user', $user);
46    $mysqli = new mysqli($host);
47    if (0 !== mysqli_connect_errno()) {
48        printf("[008] Usage of mysqli.default_user failed\n") ;
49    } else {
50        $mysqli->close();
51    }
52
53    ini_set('mysqli.default_host', $host);
54    $mysqli = new mysqli();
55    if (0 !== mysqli_connect_errno()) {
56        printf("[012] Failed to create mysqli object\n");
57    } else {
58        // There shall be NO connection! Using new mysqli(void) shall not use defaults for a connection!
59        // We had long discussions on this and found that the ext/mysqli API as
60        // such is broken. As we can't fix it, we document how it has behaved from
61        // the first day on. And that's: no connection.
62        try {
63            $mysqli->query('SELECT 1');
64        } catch (Error $exception) {
65            echo $exception->getMessage() . "\n";
66        }
67    }
68
69    ini_set('mysqli.default_host', 'p:' . $host);
70    $mysqli = new mysqli();
71    // There shall be NO connection! Using new mysqli(void) shall not use defaults for a connection!
72    // We had long discussions on this and found that the ext/mysqli API as
73    // such is broken. As we can't fix it, we document how it has behaved from
74    // the first day on. And that's: no connection.
75    try {
76        $mysqli->query('SELECT 1');
77    } catch (Error $exception) {
78        echo $exception->getMessage() . "\n";
79    }
80
81    print "... and now Exceptions\n";
82    mysqli_report(MYSQLI_REPORT_OFF);
83    mysqli_report(MYSQLI_REPORT_STRICT);
84
85    try {
86        $mysqli = new mysqli($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket);
87        printf("[016] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
88            $host, $user . 'unknown_really', $db, $port, $socket);
89        $mysqli->close();
90    } catch (mysqli_sql_exception $e) {
91        printf("%s\n", $e->getMessage());
92    }
93
94    ini_set('mysqli.default_socket', $socket);
95    try {
96        $mysqli = new mysqli($host, $user, $passwd, $db, $port);
97        $mysqli->close();
98    } catch (mysqli_sql_exception $e) {
99        printf("%s\n", $e->getMessage());
100        printf("[017] Usage of mysqli.default_socket failed\n") ;
101    }
102
103    ini_set('mysqli.default_port', $port);
104    try {
105        $mysqli = new mysqli($host, $user, $passwd, $db);
106        $mysqli->close();
107    } catch (mysqli_sql_exception $e) {
108        printf("%s\n", $e->getMessage());
109        printf("[018] Usage of mysqli.default_port failed\n") ;
110    }
111
112    ini_set('mysqli.default_pw', $passwd);
113    try {
114        $mysqli = new mysqli($host, $user);
115        $mysqli->close();
116    } catch (mysqli_sql_exception $e) {
117        printf("%s\n", $e->getMessage());
118        printf("[019] Usage of mysqli.default_pw failed\n");
119    }
120
121    ini_set('mysqli.default_user', $user);
122    try {
123        $mysqli = new mysqli($host);
124        $mysqli->close();
125    } catch (mysqli_sql_exception $e) {
126        printf("%s\n", $e->getMessage());
127        printf("[020] Usage of mysqli.default_user failed\n") ;
128    }
129
130    ini_set('mysqli.default_host', $host);
131    try {
132        /* NOTE that at this point one must use a different syntax! */
133        $mysqli = mysqli_init();
134        $mysqli->real_connect();
135        assert(0 === mysqli_connect_errno());
136        $mysqli->close();
137        assert(0 === mysqli_connect_errno());
138    } catch (mysqli_sql_exception $e) {
139        printf("%s\n", $e->getMessage());
140        printf("[021] Usage of mysqli.default_host failed\n");
141    }
142
143    print "done!";
144?>
145--EXPECTF--
146Warning: mysqli::__construct(): (%s/%d): Access denied for user '%sunknown%s'@'%s' %r(\(using password: \w+\) ){0,1}%rin %s on line %d
147mysqli object is not fully initialized
148mysqli object is not fully initialized
149... and now Exceptions
150Access denied for user '%s'@'%s'%r( \(using password: \w+\)){0,1}%r
151done!
152