1--TEST--
2mysqli_select_db()
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11    require_once("connect.inc");
12    require_once("table.inc");
13
14    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
15        printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
16            $host, $user, $db, $port, $socket);
17
18    /* does not make too much sense, unless we have access to at least one more database than $db */
19    if (!mysqli_select_db($link, $db))
20        printf("[005] Cannot select DB %s, [%d] %s\n", $db, mysqli_errno($link), mysqli_error($link));
21
22    if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
23        printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
24
25    if (!$row = mysqli_fetch_assoc($res))
26        printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
27
28    if ($row['dbname'] !== (string)$db)
29        printf("[008] Expecting database '%s', found '%s'\n", $db, $row['dbname']);
30    mysqli_free_result($res);
31
32    if (mysqli_select_db($link, 'mysql')) {
33        // Yippie, a second database to play with - that's great because mysqli_select_db
34        // ($db) was done by mysqli__connect() already and the previous test
35        // was quite useless
36        if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
37            printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
38
39        if (!$row = mysqli_fetch_assoc($res))
40            printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
41
42        if (strtolower($row['dbname']) !== 'mysql')
43            printf("[011] Expecting database 'mysql', found '%s'\n", $row['dbname']);
44        mysqli_free_result($res);
45    }
46
47    if (!$link->select_db($db))
48        printf("[012] Failed to set '%s' as current DB; [%d] %s\n", $link->errno, $link->error);
49
50    if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
51        printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
52
53    if (!$row = mysqli_fetch_assoc($res))
54        printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
55
56    $current_db = $row['dbname'];
57
58    mysqli_report(MYSQLI_REPORT_OFF);
59    mysqli_select_db($link, 'I cannot imagine that this database exists');
60    mysqli_report(MYSQLI_REPORT_ERROR);
61
62    ob_start();
63    mysqli_select_db($link, 'I cannot imagine that this database exists');
64    $output = ob_get_contents();
65    ob_end_clean();
66    if (!stristr($output, "1049") && !stristr($output, "1044") && !stristr($output, "1045")) {
67      /* Error: 1049 SQLSTATE: 42000 (ER_BAD_DB_ERROR) Message: Unknown database '%s'  */
68      /* Error: 1044 SQLSTATE: 42000 (ER_DBACCESS_DENIED_ERROR) Message: Access denied for user '%s'@'%s' to database '%s' */
69      /* Error: 1045 SQLSTATE: 28000 (ER_ACCESS_DENIED_ERROR) Message: Access denied for user '%s'@'%s' (using password: %s) */
70      echo $output;
71    }
72
73    if (!$res = mysqli_query($link, "SELECT DATABASE() AS dbname"))
74        printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
75
76    if (!$row = mysqli_fetch_assoc($res))
77        printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
78
79    if (strtolower($row['dbname']) != strtolower($current_db))
80        printf("[017] Current DB should not change if set fails\n");
81
82
83    if (!$res = $link->query("SELECT id FROM test WHERE id = 1"))
84        printf("[018] [%d] %s\n");
85
86    $row = $res->fetch_assoc();
87    $res->free();
88
89    mysqli_close($link);
90
91    try {
92        mysqli_select_db($link, $db);
93    } catch (Error $exception) {
94        echo $exception->getMessage() . "\n";
95    }
96
97    print "done!\n";
98?>
99--CLEAN--
100<?php require_once("clean_table.inc"); ?>
101--EXPECT--
102mysqli object is already closed
103done!
104