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