1--TEST--
2new mysqli()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10    require_once("connect.inc");
11
12    $tmp    = NULL;
13    $link   = NULL;
14
15    $obj = new stdClass();
16
17    if ($mysqli = new mysqli($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket) && !mysqli_connect_errno())
18        printf("[003] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n",
19            $host, $user . 'unknown_really', $db, $port, $socket);
20
21    if (false !== $mysqli)
22        printf("[004] Expecting boolean/false, got %s/%s\n", gettype($mysqli), $mysqli);
23
24    // Run the following tests without an anoynmous MySQL user and use a password for the test user!
25    ini_set('mysqli.default_socket', $socket);
26    if (!is_object($mysqli = new mysqli($host, $user, $passwd, $db, $port)) || (0 !== mysqli_connect_errno())) {
27        printf("[005] Usage of mysqli.default_socket failed\n") ;
28    } else {
29        $mysqli->close();
30    }
31
32    ini_set('mysqli.default_port', $port);
33    if (!is_object($mysqli = new mysqli($host, $user, $passwd, $db)) || (0 !== mysqli_connect_errno())) {
34        printf("[006] Usage of mysqli.default_port failed\n") ;
35    } else {
36        $mysqli->close();
37    }
38
39    ini_set('mysqli.default_pw', $passwd);
40    if (!is_object($mysqli = new mysqli($host, $user)) || (0 !== mysqli_connect_errno())) {
41        printf("[007] Usage of mysqli.default_pw failed\n") ;
42    } else {
43        $mysqli->close();
44    }
45
46    ini_set('mysqli.default_user', $user);
47    if (!is_object($mysqli = new mysqli($host)) || (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    if (!is_object($mysqli = new mysqli()) || (0 !== mysqli_connect_errno())) {
55        printf("[012] Failed to create mysqli object\n");
56    } else {
57        // There shall be NO connection! Using new mysqli(void) shall not use defaults for a connection!
58        // We had long discussions on this and found that the ext/mysqli API as
59        // such is broken. As we can't fix it, we document how it has behaved from
60        // the first day on. And that's: no connection.
61        try {
62            $mysqli->query('SELECT 1');
63        } catch (Error $exception) {
64            echo $exception->getMessage() . "\n";
65        }
66    }
67
68    if ($IS_MYSQLND) {
69        ini_set('mysqli.default_host', 'p:' . $host);
70        if (!is_object($mysqli = new mysqli())) {
71            // Due to an API flaw this shall not connect
72            printf("[010] Failed to create mysqli object\n");
73        } else {
74            // There shall be NO connection! Using new mysqli(void) shall not use defaults for a connection!
75            // We had long discussions on this and found that the ext/mysqli API as
76            // such is broken. As we can't fix it, we document how it has behaved from
77            // the first day on. And that's: no connection.
78            try {
79                $mysqli->query('SELECT 1');
80            } catch (Error $exception) {
81                echo $exception->getMessage() . "\n";
82            }
83        }
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' (using password: %s) in %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' (using password: %s)
156done!
157