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