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