reference/mysqli/mysqli/change-user.xml
ee14f82484f6289278255f798f1e8b93cdc2cab5
...
...
@@ -4,29 +4,32 @@
4
4
<refnamediv>
5
5
<refname>mysqli::change_user</refname>
6
6
<refname>mysqli_change_user</refname>
7
-
<refpurpose>Changes the user of the specified database connection</refpurpose>
7
+
<refpurpose>Changes the user of the database connection</refpurpose>
8
8
</refnamediv>
9
9

10
10
<refsect1 role="description">
11
11
&reftitle.description;
12
-
<para>Object oriented style (method):</para>
13
-
<methodsynopsis>
14
-
<type>bool</type><methodname>mysqli::change_user</methodname>
15
-
<methodparam><type>string</type><parameter>user</parameter></methodparam>
16
-
<methodparam><type>string</type><parameter>password</parameter></methodparam>
17
-
<methodparam><type>string</type><parameter>database</parameter></methodparam>
18
-
</methodsynopsis>
19
-
<para>Procedural style:</para>
12
+
<para>&style.oop;</para>
13
+
<methodsynopsis role="mysqli">
14
+
<modifier>public</modifier> <type>bool</type><methodname>mysqli::change_user</methodname>
15
+
<methodparam><type>string</type><parameter>username</parameter></methodparam>
16
+
<methodparam><modifier role="attribute">#[\SensitiveParameter]</modifier><type>string</type><parameter>password</parameter></methodparam>
17
+
<methodparam><type class="union"><type>string</type><type>null</type></type><parameter>database</parameter></methodparam>
18
+
</methodsynopsis>
19
+
<para>&style.procedural;</para>
20
20
<methodsynopsis>
21
21
<type>bool</type><methodname>mysqli_change_user</methodname>
22
-
<methodparam><type>mysqli</type><parameter>link</parameter></methodparam>
23
-
<methodparam><type>string</type><parameter>user</parameter></methodparam>
24
-
<methodparam><type>string</type><parameter>password</parameter></methodparam>
25
-
<methodparam><type>string</type><parameter>database</parameter></methodparam>
22
+
<methodparam><type>mysqli</type><parameter>mysql</parameter></methodparam>
23
+
<methodparam><type>string</type><parameter>username</parameter></methodparam>
24
+
<methodparam><modifier role="attribute">#[\SensitiveParameter]</modifier><type>string</type><parameter>password</parameter></methodparam>
25
+
<methodparam><type class="union"><type>string</type><type>null</type></type><parameter>database</parameter></methodparam>
26
26
</methodsynopsis>
27
27
<para>
28
-
Changes the user of the specified database connection and sets the current
29
-
database.
28
+
Attempts to connect to the specified database using the provided credentials.
29
+
</para>
30
+
<para>
31
+
In comparison to <methodname>mysqli::connect</methodname>, this method will
32
+
not disconnect the current connection if the new connection cannot be opened.
30
33
</para>
31
34
<para>
32
35
In order to successfully change users a valid <parameter>username</parameter> and
...
...
@@ -42,7 +45,7 @@
42
45
<variablelist>
43
46
&mysqli.link.description;
44
47
<varlistentry>
45
-
<term><parameter>user</parameter></term>
48
+
<term><parameter>username</parameter></term>
46
49
<listitem>
47
50
<para>
48
51
The MySQL user name.
...
...
@@ -61,12 +64,8 @@
61
64
<term><parameter>database</parameter></term>
62
65
<listitem>
63
66
<para>
64
-
The database to change to.
65
-
</para>
66
-
<para>
67
-
If desired, the &null; value may be passed resulting in only changing
68
-
the user and not selecting a database. To select a database in this
69
-
case use the <function>mysqli_select_db</function> function.
67
+
The database name. If &null; or an empty string is passed,
68
+
the connection to the server will be opened with no default database.
70
69
</para>
71
70
</listitem>
72
71
</varlistentry>
...
...
@@ -81,109 +80,106 @@
81
80
</para>
82
81
</refsect1>
83
82

84
-
<refsect1 role="notes">
85
-
&reftitle.notes;
86
-
<note>
87
-
<para>
88
-
Using this command will always cause the current database connection to
89
-
behave as if was a completely new database connection, regardless of if
90
-
the operation was completed successfully. This reset includes performing
91
-
a rollback on any active transactions, closing all temporary tables, and
92
-
unlocking all locked tables.
93
-
</para>
94
-
</note>
83
+
<refsect1 role="errors">
84
+
&reftitle.errors;
85
+
&mysqli.conditionalexception;
95
86
</refsect1>
96
87

97
88
<refsect1 role="examples">
98
89
&reftitle.examples;
99
-
<example>
100
-
<title>Object oriented style</title>
101
-
<programlisting role="php">
90
+
<example>
91
+
<title>Resetting the connection session</title>
92
+
<para>&style.oop;</para>
93
+
<programlisting role="php">
102
94
<![CDATA[
103
95
<?php
104
96

105
-
/* connect database test */
97
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
106
98
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
107
99

108
-
/* check connection */
109
-
if (mysqli_connect_errno()) {
110
-
printf("Connect failed: %s\n", mysqli_connect_error());
111
-
exit();
112
-
}
113
-

114
-
/* Set Variable a */
115
100
$mysqli->query("SET @a:=1");
116
101

117
-
/* reset all and select a new database */
118
102
$mysqli->change_user("my_user", "my_password", "world");
119
103

120
-
if ($result = $mysqli->query("SELECT DATABASE()")) {
121
-
$row = $result->fetch_row();
122
-
printf("Default database: %s\n", $row[0]);
123
-
$result->close();
124
-
}
104
+
$result = $mysqli->query("SELECT DATABASE()");
105
+
$row = $result->fetch_row();
106
+
printf("Default database: %s\n", $row[0]);
125
107

126
-
if ($result = $mysqli->query("SELECT @a")) {
127
-
$row = $result->fetch_row();
128
-
if ($row[0] === NULL) {
129
-
printf("Value of variable a is NULL\n");
130
-
}
131
-
$result->close();
108
+
$result = $mysqli->query("SELECT @a");
109
+
$row = $result->fetch_row();
110
+
if ($row[0] === null) {
111
+
printf("Value of variable a is NULL\n");
132
112
}
133
-

134
-
/* close connection */
135
-
$mysqli->close();
136
-
?>
137
113
]]>
138
-
</programlisting>
139
-
</example>
140
-
<example>
141
-
<title>Procedural style</title>
142
-
<programlisting role="php">
114
+
</programlisting>
115
+
<para>&style.procedural;</para>
116
+
<programlisting role="php">
143
117
<![CDATA[
144
118
<?php
145
-
/* connect database test */
146
-
$link = mysqli_connect("localhost", "my_user", "my_password", "test");
147
119

148
-
/* check connection */
149
-
if (!$link) {
150
-
printf("Connect failed: %s\n", mysqli_connect_error());
151
-
exit();
152
-
}
120
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
121
+
$link = mysqli_connect("localhost", "my_user", "my_password", "test");
153
122

154
-
/* Set Variable a */
155
123
mysqli_query($link, "SET @a:=1");
156
124

157
-
/* reset all and select a new database */
158
125
mysqli_change_user($link, "my_user", "my_password", "world");
159
126

160
-
if ($result = mysqli_query($link, "SELECT DATABASE()")) {
161
-
$row = mysqli_fetch_row($result);
162
-
printf("Default database: %s\n", $row[0]);
163
-
mysqli_free_result($result);
164
-
}
127
+
$result = mysqli_query($link, "SELECT DATABASE()");
128
+
$row = mysqli_fetch_row($result);
129
+
printf("Default database: %s\n", $row[0]);
165
130

166
-
if ($result = mysqli_query($link, "SELECT @a")) {
167
-
$row = mysqli_fetch_row($result);
168
-
if ($row[0] === NULL) {
169
-
printf("Value of variable a is NULL\n");
170
-
}
171
-
mysqli_free_result($result);
131
+
$result = mysqli_query($link, "SELECT @a");
132
+
$row = mysqli_fetch_row($result);
133
+
if ($row[0] === null) {
134
+
printf("Value of variable a is NULL\n");
172
135
}
173
-

174
-
/* close connection */
175
-
mysqli_close($link);
176
-
?>
177
136
]]>
178
137
</programlisting>
179
-
</example>
180
-
&example.outputs;
181
-
<screen>
138
+
&examples.outputs;
139
+
<screen>
182
140
<![CDATA[
183
141
Default database: world
184
142
Value of variable a is NULL
185
143
]]>
186
-
</screen>
144
+
</screen>
145
+
</example>
146
+
<example>
147
+
<title>If <parameter>database</parameter> is &null; the connection is opened without selecting any default database</title>
148
+
<para>&style.oop;</para>
149
+
<programlisting role="php">
150
+
<![CDATA[
151
+
<?php
152
+

153
+
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
154
+
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
155
+

156
+
$mysqli->change_user("my_user", "my_password", null);
157
+

158
+
$result = $mysqli->query("SELECT DATABASE()");
159
+
$row = $result->fetch_row();
160
+
printf("Default database: %s\n", $row[0]);
161
+
]]>
162
+
</programlisting>
163
+
&examples.outputs;
164
+
<screen>
165
+
<![CDATA[
166
+
Default database:
167
+
]]>
168
+
</screen>
169
+
</example>
170
+
</refsect1>
171
+

172
+
<refsect1 role="notes">
173
+
&reftitle.notes;
174
+
<note>
175
+
<para>
176
+
Using this command will always cause the current database connection to
177
+
behave as if was a completely new database connection, regardless of if
178
+
the operation was completed successfully. This reset includes performing
179
+
a rollback on any active transactions, closing all temporary tables, and
180
+
unlocking all locked tables.
181
+
</para>
182
+
</note>
187
183
</refsect1>
188
184

189
185
<refsect1 role="seealso">
...
...
@@ -197,7 +193,6 @@ Value of variable a is NULL
197
193
</refsect1>
198
194

199
195
</refentry>
200
-

201
196
<!-- Keep this comment at the end of the file
202
197
Local variables:
203
198
mode: sgml
204
199