| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\MysqlException; |
| 6 |
|
| 7 |
/** |
| 8 |
* mysql_close closes the non-persistent connection to |
| 9 |
* the MySQL server that's associated with the specified link identifier. If |
| 10 |
* link_identifier isn't specified, the last opened |
| 11 |
* link is used. |
| 12 |
* |
| 13 |
* |
| 14 |
* Open non-persistent MySQL connections and result sets are automatically destroyed when a |
| 15 |
* PHP script finishes its execution. So, while explicitly closing open |
| 16 |
* connections and freeing result sets is optional, doing so is recommended. |
| 17 |
* This will immediately return resources to PHP and MySQL, which can |
| 18 |
* improve performance. For related information, see |
| 19 |
* freeing resources |
| 20 |
* |
| 21 |
* @param resource $link_identifier The MySQL connection. If the |
| 22 |
* link identifier is not specified, the last link opened by |
| 23 |
* mysql_connect is assumed. If no connection is found or |
| 24 |
* established, an E_WARNING level error is |
| 25 |
* generated. |
| 26 |
* @throws MysqlException |
| 27 |
* |
| 28 |
*/ |
| 29 |
function mysql_close($link_identifier = null): void |
| 30 |
{ |
| 31 |
error_clear_last(); |
| 32 |
$result = \mysql_close($link_identifier); |
| 33 |
if ($result === false) { |
| 34 |
throw MysqlException::createFromPhpError(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* Opens or reuses a connection to a MySQL server. |
| 41 |
* |
| 42 |
* @param string|null $server The MySQL server. It can also include a port number. e.g. |
| 43 |
* "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for |
| 44 |
* the localhost. |
| 45 |
* |
| 46 |
* If the PHP directive |
| 47 |
* mysql.default_host is undefined (default), then the default |
| 48 |
* value is 'localhost:3306'. In SQL safe mode, this parameter is ignored |
| 49 |
* and value 'localhost:3306' is always used. |
| 50 |
* @param string|null $username The username. Default value is defined by mysql.default_user. In |
| 51 |
* SQL safe mode, this parameter is ignored and the name of the user that |
| 52 |
* owns the server process is used. |
| 53 |
* @param string|null $password The password. Default value is defined by mysql.default_password. In |
| 54 |
* SQL safe mode, this parameter is ignored and empty password is used. |
| 55 |
* @param bool $new_link If a second call is made to mysql_connect |
| 56 |
* with the same arguments, no new link will be established, but |
| 57 |
* instead, the link identifier of the already opened link will be |
| 58 |
* returned. The new_link parameter modifies this |
| 59 |
* behavior and makes mysql_connect always open |
| 60 |
* a new link, even if mysql_connect was called |
| 61 |
* before with the same parameters. |
| 62 |
* In SQL safe mode, this parameter is ignored. |
| 63 |
* @param int $client_flags The client_flags parameter can be a combination |
| 64 |
* of the following constants: |
| 65 |
* 128 (enable LOAD DATA LOCAL handling), |
| 66 |
* MYSQL_CLIENT_SSL, |
| 67 |
* MYSQL_CLIENT_COMPRESS, |
| 68 |
* MYSQL_CLIENT_IGNORE_SPACE or |
| 69 |
* MYSQL_CLIENT_INTERACTIVE. |
| 70 |
* Read the section about for further information. |
| 71 |
* In SQL safe mode, this parameter is ignored. |
| 72 |
* |
| 73 |
* @return resource Returns a MySQL link identifier on success. |
| 74 |
* @throws MysqlException |
| 75 |
* |
| 76 |
*/ |
| 77 |
function mysql_connect( ?string $server = null, ?string $username = null, ?string $password = null, bool $new_link = false, int $client_flags = 0) |
| 78 |
{ |
| 79 |
error_clear_last(); |
| 80 |
if ($client_flags !== 0) { |
| 81 |
$result = \mysql_connect($server, $username, $password, $new_link, $client_flags); |
| 82 |
} elseif ($new_link !== false) { |
| 83 |
$result = \mysql_connect($server, $username, $password, $new_link); |
| 84 |
} elseif ($password !== null) { |
| 85 |
$result = \mysql_connect($server, $username, $password); |
| 86 |
} elseif ($username !== null) { |
| 87 |
$result = \mysql_connect($server, $username); |
| 88 |
} elseif ($server !== null) { |
| 89 |
$result = \mysql_connect($server); |
| 90 |
} else { |
| 91 |
$result = \mysql_connect(); |
| 92 |
} |
| 93 |
if ($result === false) { |
| 94 |
throw MysqlException::createFromPhpError(); |
| 95 |
} |
| 96 |
return $result; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* mysql_create_db attempts to create a new |
| 102 |
* database on the server associated with the specified link |
| 103 |
* identifier. |
| 104 |
* |
| 105 |
* @param string $database_name The name of the database being created. |
| 106 |
* @param resource $link_identifier The MySQL connection. If the |
| 107 |
* link identifier is not specified, the last link opened by |
| 108 |
* mysql_connect is assumed. If no such link is found, it |
| 109 |
* will try to create one as if mysql_connect had been called |
| 110 |
* with no arguments. If no connection is found or established, an |
| 111 |
* E_WARNING level error is generated. |
| 112 |
* @throws MysqlException |
| 113 |
* |
| 114 |
*/ |
| 115 |
function mysql_create_db(string $database_name, $link_identifier = null): void |
| 116 |
{ |
| 117 |
error_clear_last(); |
| 118 |
$result = \mysql_create_db($database_name, $link_identifier); |
| 119 |
if ($result === false) { |
| 120 |
throw MysqlException::createFromPhpError(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* mysql_data_seek moves the internal row |
| 127 |
* pointer of the MySQL result associated with the specified result |
| 128 |
* identifier to point to the specified row number. The next call |
| 129 |
* to a MySQL fetch function, such as mysql_fetch_assoc, |
| 130 |
* would return that row. |
| 131 |
* |
| 132 |
* row_number starts at 0. The |
| 133 |
* row_number should be a value in the range from 0 to |
| 134 |
* mysql_num_rows - 1. However if the result set |
| 135 |
* is empty (mysql_num_rows == 0), a seek to 0 will |
| 136 |
* fail with an E_WARNING and |
| 137 |
* mysql_data_seek will return FALSE. |
| 138 |
* |
| 139 |
* @param resource $result The result resource that |
| 140 |
* is being evaluated. This result comes from a call to |
| 141 |
* mysql_query. |
| 142 |
* @param int $row_number The desired row number of the new result pointer. |
| 143 |
* @throws MysqlException |
| 144 |
* |
| 145 |
*/ |
| 146 |
function mysql_data_seek($result, int $row_number): void |
| 147 |
{ |
| 148 |
error_clear_last(); |
| 149 |
$result = \mysql_data_seek($result, $row_number); |
| 150 |
if ($result === false) { |
| 151 |
throw MysqlException::createFromPhpError(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
/** |
| 157 |
* Retrieve the database name from a call to |
| 158 |
* mysql_list_dbs. |
| 159 |
* |
| 160 |
* @param resource $result The result pointer from a call to mysql_list_dbs. |
| 161 |
* @param int $row The index into the result set. |
| 162 |
* @param mixed $field The field name. |
| 163 |
* @return string Returns the database name on success. If FALSE |
| 164 |
* is returned, use mysql_error to determine the nature |
| 165 |
* of the error. |
| 166 |
* @throws MysqlException |
| 167 |
* |
| 168 |
*/ |
| 169 |
function mysql_db_name($result, int $row, $field = null): string |
| 170 |
{ |
| 171 |
error_clear_last(); |
| 172 |
$result = \mysql_db_name($result, $row, $field); |
| 173 |
if ($result === false) { |
| 174 |
throw MysqlException::createFromPhpError(); |
| 175 |
} |
| 176 |
return $result; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
/** |
| 181 |
* mysql_db_query selects a database, and executes a |
| 182 |
* query on it. |
| 183 |
* |
| 184 |
* @param string $database The name of the database that will be selected. |
| 185 |
* @param string $query The MySQL query. |
| 186 |
* |
| 187 |
* Data inside the query should be properly escaped. |
| 188 |
* @param resource $link_identifier The MySQL connection. If the |
| 189 |
* link identifier is not specified, the last link opened by |
| 190 |
* mysql_connect is assumed. If no such link is found, it |
| 191 |
* will try to create one as if mysql_connect had been called |
| 192 |
* with no arguments. If no connection is found or established, an |
| 193 |
* E_WARNING level error is generated. |
| 194 |
* @return resource|bool Returns a positive MySQL result resource to the query result. The function also returns TRUE/FALSE for |
| 195 |
* INSERT/UPDATE/DELETE |
| 196 |
* queries to indicate success/failure. |
| 197 |
* @throws MysqlException |
| 198 |
* |
| 199 |
*/ |
| 200 |
function mysql_db_query(string $database, string $query, $link_identifier = null) |
| 201 |
{ |
| 202 |
error_clear_last(); |
| 203 |
$result = \mysql_db_query($database, $query, $link_identifier); |
| 204 |
if ($result === false) { |
| 205 |
throw MysqlException::createFromPhpError(); |
| 206 |
} |
| 207 |
return $result; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
/** |
| 212 |
* mysql_drop_db attempts to drop (remove) an |
| 213 |
* entire database from the server associated with the specified |
| 214 |
* link identifier. This function is deprecated, it is preferable to use |
| 215 |
* mysql_query to issue an sql |
| 216 |
* DROP DATABASE statement instead. |
| 217 |
* |
| 218 |
* @param string $database_name The name of the database that will be deleted. |
| 219 |
* @param resource $link_identifier The MySQL connection. If the |
| 220 |
* link identifier is not specified, the last link opened by |
| 221 |
* mysql_connect is assumed. If no such link is found, it |
| 222 |
* will try to create one as if mysql_connect had been called |
| 223 |
* with no arguments. If no connection is found or established, an |
| 224 |
* E_WARNING level error is generated. |
| 225 |
* @throws MysqlException |
| 226 |
* |
| 227 |
*/ |
| 228 |
function mysql_drop_db(string $database_name, $link_identifier = null): void |
| 229 |
{ |
| 230 |
error_clear_last(); |
| 231 |
$result = \mysql_drop_db($database_name, $link_identifier); |
| 232 |
if ($result === false) { |
| 233 |
throw MysqlException::createFromPhpError(); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* Returns an array that corresponds to the lengths of each field |
| 240 |
* in the last row fetched by MySQL. |
| 241 |
* |
| 242 |
* mysql_fetch_lengths stores the lengths of |
| 243 |
* each result column in the last row returned by |
| 244 |
* mysql_fetch_row, |
| 245 |
* mysql_fetch_assoc, |
| 246 |
* mysql_fetch_array, and |
| 247 |
* mysql_fetch_object in an array, starting at |
| 248 |
* offset 0. |
| 249 |
* |
| 250 |
* @param resource $result The result resource that |
| 251 |
* is being evaluated. This result comes from a call to |
| 252 |
* mysql_query. |
| 253 |
* @return array An array of lengths on success. |
| 254 |
* @throws MysqlException |
| 255 |
* |
| 256 |
*/ |
| 257 |
function mysql_fetch_lengths($result): array |
| 258 |
{ |
| 259 |
error_clear_last(); |
| 260 |
$result = \mysql_fetch_lengths($result); |
| 261 |
if ($result === false) { |
| 262 |
throw MysqlException::createFromPhpError(); |
| 263 |
} |
| 264 |
return $result; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
/** |
| 269 |
* mysql_field_flags returns the field flags of |
| 270 |
* the specified field. The flags are reported as a single word |
| 271 |
* per flag separated by a single space, so that you can split the |
| 272 |
* returned value using explode. |
| 273 |
* |
| 274 |
* @param resource $result The result resource that |
| 275 |
* is being evaluated. This result comes from a call to |
| 276 |
* mysql_query. |
| 277 |
* @param int $field_offset The numerical field offset. The |
| 278 |
* field_offset starts at 0. If |
| 279 |
* field_offset does not exist, an error of level |
| 280 |
* E_WARNING is also issued. |
| 281 |
* @return string Returns a string of flags associated with the result. |
| 282 |
* |
| 283 |
* The following flags are reported, if your version of MySQL |
| 284 |
* is current enough to support them: "not_null", |
| 285 |
* "primary_key", "unique_key", |
| 286 |
* "multiple_key", "blob", |
| 287 |
* "unsigned", "zerofill", |
| 288 |
* "binary", "enum", |
| 289 |
* "auto_increment" and "timestamp". |
| 290 |
* @throws MysqlException |
| 291 |
* |
| 292 |
*/ |
| 293 |
function mysql_field_flags($result, int $field_offset): string |
| 294 |
{ |
| 295 |
error_clear_last(); |
| 296 |
$result = \mysql_field_flags($result, $field_offset); |
| 297 |
if ($result === false) { |
| 298 |
throw MysqlException::createFromPhpError(); |
| 299 |
} |
| 300 |
return $result; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
/** |
| 305 |
* mysql_field_len returns the length of the |
| 306 |
* specified field. |
| 307 |
* |
| 308 |
* @param resource $result The result resource that |
| 309 |
* is being evaluated. This result comes from a call to |
| 310 |
* mysql_query. |
| 311 |
* @param int $field_offset The numerical field offset. The |
| 312 |
* field_offset starts at 0. If |
| 313 |
* field_offset does not exist, an error of level |
| 314 |
* E_WARNING is also issued. |
| 315 |
* @return int The length of the specified field index on success. |
| 316 |
* @throws MysqlException |
| 317 |
* |
| 318 |
*/ |
| 319 |
function mysql_field_len($result, int $field_offset): int |
| 320 |
{ |
| 321 |
error_clear_last(); |
| 322 |
$result = \mysql_field_len($result, $field_offset); |
| 323 |
if ($result === false) { |
| 324 |
throw MysqlException::createFromPhpError(); |
| 325 |
} |
| 326 |
return $result; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
/** |
| 331 |
* mysql_field_name returns the name of the |
| 332 |
* specified field index. |
| 333 |
* |
| 334 |
* @param resource $result The result resource that |
| 335 |
* is being evaluated. This result comes from a call to |
| 336 |
* mysql_query. |
| 337 |
* @param int $field_offset The numerical field offset. The |
| 338 |
* field_offset starts at 0. If |
| 339 |
* field_offset does not exist, an error of level |
| 340 |
* E_WARNING is also issued. |
| 341 |
* @return string The name of the specified field index on success. |
| 342 |
* @throws MysqlException |
| 343 |
* |
| 344 |
*/ |
| 345 |
function mysql_field_name($result, int $field_offset): string |
| 346 |
{ |
| 347 |
error_clear_last(); |
| 348 |
$result = \mysql_field_name($result, $field_offset); |
| 349 |
if ($result === false) { |
| 350 |
throw MysqlException::createFromPhpError(); |
| 351 |
} |
| 352 |
return $result; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
/** |
| 357 |
* Seeks to the specified field offset. If the next call to |
| 358 |
* mysql_fetch_field doesn't include a field |
| 359 |
* offset, the field offset specified in |
| 360 |
* mysql_field_seek will be returned. |
| 361 |
* |
| 362 |
* @param resource $result The result resource that |
| 363 |
* is being evaluated. This result comes from a call to |
| 364 |
* mysql_query. |
| 365 |
* @param int $field_offset The numerical field offset. The |
| 366 |
* field_offset starts at 0. If |
| 367 |
* field_offset does not exist, an error of level |
| 368 |
* E_WARNING is also issued. |
| 369 |
* @throws MysqlException |
| 370 |
* |
| 371 |
*/ |
| 372 |
function mysql_field_seek($result, int $field_offset): void |
| 373 |
{ |
| 374 |
error_clear_last(); |
| 375 |
$result = \mysql_field_seek($result, $field_offset); |
| 376 |
if ($result === false) { |
| 377 |
throw MysqlException::createFromPhpError(); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
/** |
| 383 |
* mysql_free_result will free all memory |
| 384 |
* associated with the result identifier result. |
| 385 |
* |
| 386 |
* mysql_free_result only needs to be called if |
| 387 |
* you are concerned about how much memory is being used for queries |
| 388 |
* that return large result sets. All associated result memory is |
| 389 |
* automatically freed at the end of the script's execution. |
| 390 |
* |
| 391 |
* @param resource $result The result resource that |
| 392 |
* is being evaluated. This result comes from a call to |
| 393 |
* mysql_query. |
| 394 |
* @throws MysqlException |
| 395 |
* |
| 396 |
*/ |
| 397 |
function mysql_free_result($result): void |
| 398 |
{ |
| 399 |
error_clear_last(); |
| 400 |
$result = \mysql_free_result($result); |
| 401 |
if ($result === false) { |
| 402 |
throw MysqlException::createFromPhpError(); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
/** |
| 408 |
* Describes the type of connection in use for the connection, including the |
| 409 |
* server host name. |
| 410 |
* |
| 411 |
* @param resource $link_identifier The MySQL connection. If the |
| 412 |
* link identifier is not specified, the last link opened by |
| 413 |
* mysql_connect is assumed. If no such link is found, it |
| 414 |
* will try to create one as if mysql_connect had been called |
| 415 |
* with no arguments. If no connection is found or established, an |
| 416 |
* E_WARNING level error is generated. |
| 417 |
* @return string Returns a string describing the type of MySQL connection in use for the |
| 418 |
* connection. |
| 419 |
* @throws MysqlException |
| 420 |
* |
| 421 |
*/ |
| 422 |
function mysql_get_host_info($link_identifier = null): string |
| 423 |
{ |
| 424 |
error_clear_last(); |
| 425 |
$result = \mysql_get_host_info($link_identifier); |
| 426 |
if ($result === false) { |
| 427 |
throw MysqlException::createFromPhpError(); |
| 428 |
} |
| 429 |
return $result; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
/** |
| 434 |
* Retrieves the MySQL protocol. |
| 435 |
* |
| 436 |
* @param resource $link_identifier The MySQL connection. If the |
| 437 |
* link identifier is not specified, the last link opened by |
| 438 |
* mysql_connect is assumed. If no such link is found, it |
| 439 |
* will try to create one as if mysql_connect had been called |
| 440 |
* with no arguments. If no connection is found or established, an |
| 441 |
* E_WARNING level error is generated. |
| 442 |
* @return int Returns the MySQL protocol on success. |
| 443 |
* @throws MysqlException |
| 444 |
* |
| 445 |
*/ |
| 446 |
function mysql_get_proto_info($link_identifier = null): int |
| 447 |
{ |
| 448 |
error_clear_last(); |
| 449 |
$result = \mysql_get_proto_info($link_identifier); |
| 450 |
if ($result === false) { |
| 451 |
throw MysqlException::createFromPhpError(); |
| 452 |
} |
| 453 |
return $result; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
/** |
| 458 |
* Retrieves the MySQL server version. |
| 459 |
* |
| 460 |
* @param resource $link_identifier The MySQL connection. If the |
| 461 |
* link identifier is not specified, the last link opened by |
| 462 |
* mysql_connect is assumed. If no such link is found, it |
| 463 |
* will try to create one as if mysql_connect had been called |
| 464 |
* with no arguments. If no connection is found or established, an |
| 465 |
* E_WARNING level error is generated. |
| 466 |
* @return string Returns the MySQL server version on success. |
| 467 |
* @throws MysqlException |
| 468 |
* |
| 469 |
*/ |
| 470 |
function mysql_get_server_info($link_identifier = null): string |
| 471 |
{ |
| 472 |
error_clear_last(); |
| 473 |
$result = \mysql_get_server_info($link_identifier); |
| 474 |
if ($result === false) { |
| 475 |
throw MysqlException::createFromPhpError(); |
| 476 |
} |
| 477 |
return $result; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
/** |
| 482 |
* Returns detailed information about the last query. |
| 483 |
* |
| 484 |
* @param resource $link_identifier The MySQL connection. If the |
| 485 |
* link identifier is not specified, the last link opened by |
| 486 |
* mysql_connect is assumed. If no such link is found, it |
| 487 |
* will try to create one as if mysql_connect had been called |
| 488 |
* with no arguments. If no connection is found or established, an |
| 489 |
* E_WARNING level error is generated. |
| 490 |
* @return string Returns information about the statement on success. See the example below for which statements provide information, |
| 491 |
* and what the returned value may look like. Statements that are not listed |
| 492 |
* will return FALSE. |
| 493 |
* @throws MysqlException |
| 494 |
* |
| 495 |
*/ |
| 496 |
function mysql_info($link_identifier = null): string |
| 497 |
{ |
| 498 |
error_clear_last(); |
| 499 |
$result = \mysql_info($link_identifier); |
| 500 |
if ($result === false) { |
| 501 |
throw MysqlException::createFromPhpError(); |
| 502 |
} |
| 503 |
return $result; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
/** |
| 508 |
* Returns a result pointer containing the databases available from the |
| 509 |
* current mysql daemon. |
| 510 |
* |
| 511 |
* @param resource $link_identifier The MySQL connection. If the |
| 512 |
* link identifier is not specified, the last link opened by |
| 513 |
* mysql_connect is assumed. If no such link is found, it |
| 514 |
* will try to create one as if mysql_connect had been called |
| 515 |
* with no arguments. If no connection is found or established, an |
| 516 |
* E_WARNING level error is generated. |
| 517 |
* @return resource Returns a result pointer resource on success. Use the mysql_tablename function to traverse |
| 518 |
* this result pointer, or any function for result tables, such as |
| 519 |
* mysql_fetch_array. |
| 520 |
* @throws MysqlException |
| 521 |
* |
| 522 |
*/ |
| 523 |
function mysql_list_dbs($link_identifier = null) |
| 524 |
{ |
| 525 |
error_clear_last(); |
| 526 |
$result = \mysql_list_dbs($link_identifier); |
| 527 |
if ($result === false) { |
| 528 |
throw MysqlException::createFromPhpError(); |
| 529 |
} |
| 530 |
return $result; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
/** |
| 535 |
* Retrieves information about the given table name. |
| 536 |
* |
| 537 |
* This function is deprecated. It is preferable to use |
| 538 |
* mysql_query to issue an SQL SHOW COLUMNS FROM |
| 539 |
* table [LIKE 'name'] statement instead. |
| 540 |
* |
| 541 |
* @param string $database_name The name of the database that's being queried. |
| 542 |
* @param string $table_name The name of the table that's being queried. |
| 543 |
* @param resource $link_identifier The MySQL connection. If the |
| 544 |
* link identifier is not specified, the last link opened by |
| 545 |
* mysql_connect is assumed. If no such link is found, it |
| 546 |
* will try to create one as if mysql_connect had been called |
| 547 |
* with no arguments. If no connection is found or established, an |
| 548 |
* E_WARNING level error is generated. |
| 549 |
* @return resource A result pointer resource on success. |
| 550 |
* |
| 551 |
* The returned result can be used with mysql_field_flags, |
| 552 |
* mysql_field_len, |
| 553 |
* mysql_field_name and |
| 554 |
* mysql_field_type. |
| 555 |
* @throws MysqlException |
| 556 |
* |
| 557 |
*/ |
| 558 |
function mysql_list_fields(string $database_name, string $table_name, $link_identifier = null) |
| 559 |
{ |
| 560 |
error_clear_last(); |
| 561 |
$result = \mysql_list_fields($database_name, $table_name, $link_identifier); |
| 562 |
if ($result === false) { |
| 563 |
throw MysqlException::createFromPhpError(); |
| 564 |
} |
| 565 |
return $result; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
/** |
| 570 |
* Retrieves the current MySQL server threads. |
| 571 |
* |
| 572 |
* @param resource $link_identifier The MySQL connection. If the |
| 573 |
* link identifier is not specified, the last link opened by |
| 574 |
* mysql_connect is assumed. If no such link is found, it |
| 575 |
* will try to create one as if mysql_connect had been called |
| 576 |
* with no arguments. If no connection is found or established, an |
| 577 |
* E_WARNING level error is generated. |
| 578 |
* @return resource A result pointer resource on success. |
| 579 |
* @throws MysqlException |
| 580 |
* |
| 581 |
*/ |
| 582 |
function mysql_list_processes($link_identifier = null) |
| 583 |
{ |
| 584 |
error_clear_last(); |
| 585 |
$result = \mysql_list_processes($link_identifier); |
| 586 |
if ($result === false) { |
| 587 |
throw MysqlException::createFromPhpError(); |
| 588 |
} |
| 589 |
return $result; |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
/** |
| 594 |
* Retrieves a list of table names from a MySQL database. |
| 595 |
* |
| 596 |
* This function is deprecated. It is preferable to use |
| 597 |
* mysql_query to issue an SQL SHOW TABLES |
| 598 |
* [FROM db_name] [LIKE 'pattern'] statement instead. |
| 599 |
* |
| 600 |
* @param string $database The name of the database |
| 601 |
* @param resource $link_identifier The MySQL connection. If the |
| 602 |
* link identifier is not specified, the last link opened by |
| 603 |
* mysql_connect is assumed. If no such link is found, it |
| 604 |
* will try to create one as if mysql_connect had been called |
| 605 |
* with no arguments. If no connection is found or established, an |
| 606 |
* E_WARNING level error is generated. |
| 607 |
* @return resource A result pointer resource on success. |
| 608 |
* |
| 609 |
* Use the mysql_tablename function to |
| 610 |
* traverse this result pointer, or any function for result tables, |
| 611 |
* such as mysql_fetch_array. |
| 612 |
* @throws MysqlException |
| 613 |
* |
| 614 |
*/ |
| 615 |
function mysql_list_tables(string $database, $link_identifier = null) |
| 616 |
{ |
| 617 |
error_clear_last(); |
| 618 |
$result = \mysql_list_tables($database, $link_identifier); |
| 619 |
if ($result === false) { |
| 620 |
throw MysqlException::createFromPhpError(); |
| 621 |
} |
| 622 |
return $result; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
/** |
| 627 |
* Retrieves the number of fields from a query. |
| 628 |
* |
| 629 |
* @param resource $result The result resource that |
| 630 |
* is being evaluated. This result comes from a call to |
| 631 |
* mysql_query. |
| 632 |
* @return int Returns the number of fields in the result set resource on |
| 633 |
* success. |
| 634 |
* @throws MysqlException |
| 635 |
* |
| 636 |
*/ |
| 637 |
function mysql_num_fields($result): int |
| 638 |
{ |
| 639 |
error_clear_last(); |
| 640 |
$result = \mysql_num_fields($result); |
| 641 |
if ($result === false) { |
| 642 |
throw MysqlException::createFromPhpError(); |
| 643 |
} |
| 644 |
return $result; |
| 645 |
} |
| 646 |
|
| 647 |
|
| 648 |
/** |
| 649 |
* Retrieves the number of rows from a result set. This command is only valid |
| 650 |
* for statements like SELECT or SHOW that return an actual result set. |
| 651 |
* To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or |
| 652 |
* DELETE query, use mysql_affected_rows. |
| 653 |
* |
| 654 |
* @param resource $result The result resource that |
| 655 |
* is being evaluated. This result comes from a call to |
| 656 |
* mysql_query. |
| 657 |
* @return int The number of rows in a result set on success. |
| 658 |
* @throws MysqlException |
| 659 |
* |
| 660 |
*/ |
| 661 |
function mysql_num_rows($result): int |
| 662 |
{ |
| 663 |
error_clear_last(); |
| 664 |
$result = \mysql_num_rows($result); |
| 665 |
if ($result === false) { |
| 666 |
throw MysqlException::createFromPhpError(); |
| 667 |
} |
| 668 |
return $result; |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
/** |
| 673 |
* mysql_query sends a unique query (multiple queries |
| 674 |
* are not supported) to the currently |
| 675 |
* active database on the server that's associated with the |
| 676 |
* specified link_identifier. |
| 677 |
* |
| 678 |
* @param string $query An SQL query |
| 679 |
* |
| 680 |
* The query string should not end with a semicolon. |
| 681 |
* Data inside the query should be properly escaped. |
| 682 |
* @param resource $link_identifier The MySQL connection. If the |
| 683 |
* link identifier is not specified, the last link opened by |
| 684 |
* mysql_connect is assumed. If no such link is found, it |
| 685 |
* will try to create one as if mysql_connect had been called |
| 686 |
* with no arguments. If no connection is found or established, an |
| 687 |
* E_WARNING level error is generated. |
| 688 |
* @return resource|bool For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, |
| 689 |
* mysql_query |
| 690 |
* returns a resource on success. |
| 691 |
* |
| 692 |
* For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, |
| 693 |
* mysql_query returns TRUE on success. |
| 694 |
* |
| 695 |
* The returned result resource should be passed to |
| 696 |
* mysql_fetch_array, and other |
| 697 |
* functions for dealing with result tables, to access the returned data. |
| 698 |
* |
| 699 |
* Use mysql_num_rows to find out how many rows |
| 700 |
* were returned for a SELECT statement or |
| 701 |
* mysql_affected_rows to find out how many |
| 702 |
* rows were affected by a DELETE, INSERT, REPLACE, or UPDATE |
| 703 |
* statement. |
| 704 |
* |
| 705 |
* mysql_query will also fail and return FALSE |
| 706 |
* if the user does not have permission to access the table(s) referenced by |
| 707 |
* the query. |
| 708 |
* @throws MysqlException |
| 709 |
* |
| 710 |
*/ |
| 711 |
function mysql_query(string $query, $link_identifier = null) |
| 712 |
{ |
| 713 |
error_clear_last(); |
| 714 |
$result = \mysql_query($query, $link_identifier); |
| 715 |
if ($result === false) { |
| 716 |
throw MysqlException::createFromPhpError(); |
| 717 |
} |
| 718 |
return $result; |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
/** |
| 723 |
* Escapes special characters in the unescaped_string, |
| 724 |
* taking into account the current character set of the connection so that it |
| 725 |
* is safe to place it in a mysql_query. If binary data |
| 726 |
* is to be inserted, this function must be used. |
| 727 |
* |
| 728 |
* mysql_real_escape_string calls MySQL's library function |
| 729 |
* mysql_real_escape_string, which prepends backslashes to the following characters: |
| 730 |
* \x00, \n, |
| 731 |
* \r, \, ', |
| 732 |
* " and \x1a. |
| 733 |
* |
| 734 |
* This function must always (with few exceptions) be used to make data |
| 735 |
* safe before sending a query to MySQL. |
| 736 |
* |
| 737 |
* @param string $unescaped_string The string that is to be escaped. |
| 738 |
* @param resource $link_identifier The MySQL connection. If the |
| 739 |
* link identifier is not specified, the last link opened by |
| 740 |
* mysql_connect is assumed. If no such link is found, it |
| 741 |
* will try to create one as if mysql_connect had been called |
| 742 |
* with no arguments. If no connection is found or established, an |
| 743 |
* E_WARNING level error is generated. |
| 744 |
* @return string Returns the escaped string. |
| 745 |
* @throws MysqlException |
| 746 |
* |
| 747 |
*/ |
| 748 |
function mysql_real_escape_string(string $unescaped_string, $link_identifier = null): string |
| 749 |
{ |
| 750 |
error_clear_last(); |
| 751 |
$result = \mysql_real_escape_string($unescaped_string, $link_identifier); |
| 752 |
if ($result === false) { |
| 753 |
throw MysqlException::createFromPhpError(); |
| 754 |
} |
| 755 |
return $result; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
/** |
| 760 |
* Retrieves the contents of one cell from a MySQL result set. |
| 761 |
* |
| 762 |
* When working on large result sets, you should consider using one |
| 763 |
* of the functions that fetch an entire row (specified below). As |
| 764 |
* these functions return the contents of multiple cells in one |
| 765 |
* function call, they're MUCH quicker than |
| 766 |
* mysql_result. Also, note that specifying a |
| 767 |
* numeric offset for the field argument is much quicker than |
| 768 |
* specifying a fieldname or tablename.fieldname argument. |
| 769 |
* |
| 770 |
* @param resource $result The result resource that |
| 771 |
* is being evaluated. This result comes from a call to |
| 772 |
* mysql_query. |
| 773 |
* @param int $row The row number from the result that's being retrieved. Row numbers |
| 774 |
* start at 0. |
| 775 |
* @param mixed $field The name or offset of the field being retrieved. |
| 776 |
* |
| 777 |
* It can be the field's offset, the field's name, or the field's table |
| 778 |
* dot field name (tablename.fieldname). If the column name has been |
| 779 |
* aliased ('select foo as bar from...'), use the alias instead of the |
| 780 |
* column name. If undefined, the first field is retrieved. |
| 781 |
* @return string The contents of one cell from a MySQL result set on success. |
| 782 |
* @throws MysqlException |
| 783 |
* |
| 784 |
*/ |
| 785 |
function mysql_result($result, int $row, $field = 0): string |
| 786 |
{ |
| 787 |
error_clear_last(); |
| 788 |
$result = \mysql_result($result, $row, $field); |
| 789 |
if ($result === false) { |
| 790 |
throw MysqlException::createFromPhpError(); |
| 791 |
} |
| 792 |
return $result; |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
/** |
| 797 |
* Sets the current active database on the server that's associated with the |
| 798 |
* specified link identifier. Every subsequent call to |
| 799 |
* mysql_query will be made on the active database. |
| 800 |
* |
| 801 |
* @param string $database_name The name of the database that is to be selected. |
| 802 |
* @param resource $link_identifier The MySQL connection. If the |
| 803 |
* link identifier is not specified, the last link opened by |
| 804 |
* mysql_connect is assumed. If no such link is found, it |
| 805 |
* will try to create one as if mysql_connect had been called |
| 806 |
* with no arguments. If no connection is found or established, an |
| 807 |
* E_WARNING level error is generated. |
| 808 |
* @throws MysqlException |
| 809 |
* |
| 810 |
*/ |
| 811 |
function mysql_select_db(string $database_name, $link_identifier = null): void |
| 812 |
{ |
| 813 |
error_clear_last(); |
| 814 |
$result = \mysql_select_db($database_name, $link_identifier); |
| 815 |
if ($result === false) { |
| 816 |
throw MysqlException::createFromPhpError(); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
/** |
| 822 |
* Sets the default character set for the current connection. |
| 823 |
* |
| 824 |
* @param string $charset A valid character set name. |
| 825 |
* @param resource $link_identifier The MySQL connection. If the |
| 826 |
* link identifier is not specified, the last link opened by |
| 827 |
* mysql_connect is assumed. If no such link is found, it |
| 828 |
* will try to create one as if mysql_connect had been called |
| 829 |
* with no arguments. If no connection is found or established, an |
| 830 |
* E_WARNING level error is generated. |
| 831 |
* @throws MysqlException |
| 832 |
* |
| 833 |
*/ |
| 834 |
function mysql_set_charset(string $charset, $link_identifier = null): void |
| 835 |
{ |
| 836 |
error_clear_last(); |
| 837 |
$result = \mysql_set_charset($charset, $link_identifier); |
| 838 |
if ($result === false) { |
| 839 |
throw MysqlException::createFromPhpError(); |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
/** |
| 845 |
* Retrieves the table name from a result. |
| 846 |
* |
| 847 |
* This function is deprecated. It is preferable to use |
| 848 |
* mysql_query to issue an SQL SHOW TABLES |
| 849 |
* [FROM db_name] [LIKE 'pattern'] statement instead. |
| 850 |
* |
| 851 |
* @param resource $result A result pointer resource that's returned from |
| 852 |
* mysql_list_tables. |
| 853 |
* @param int $i The integer index (row/table number) |
| 854 |
* @return string The name of the table on success. |
| 855 |
* |
| 856 |
* Use the mysql_tablename function to |
| 857 |
* traverse this result pointer, or any function for result tables, |
| 858 |
* such as mysql_fetch_array. |
| 859 |
* @throws MysqlException |
| 860 |
* |
| 861 |
*/ |
| 862 |
function mysql_tablename($result, int $i): string |
| 863 |
{ |
| 864 |
error_clear_last(); |
| 865 |
$result = \mysql_tablename($result, $i); |
| 866 |
if ($result === false) { |
| 867 |
throw MysqlException::createFromPhpError(); |
| 868 |
} |
| 869 |
return $result; |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
/** |
| 874 |
* Retrieves the current thread ID. If the connection is lost, and a reconnect |
| 875 |
* with mysql_ping is executed, the thread ID will |
| 876 |
* change. This means only retrieve the thread ID when needed. |
| 877 |
* |
| 878 |
* @param resource $link_identifier The MySQL connection. If the |
| 879 |
* link identifier is not specified, the last link opened by |
| 880 |
* mysql_connect is assumed. If no such link is found, it |
| 881 |
* will try to create one as if mysql_connect had been called |
| 882 |
* with no arguments. If no connection is found or established, an |
| 883 |
* E_WARNING level error is generated. |
| 884 |
* @return int The thread ID on success. |
| 885 |
* @throws MysqlException |
| 886 |
* |
| 887 |
*/ |
| 888 |
function mysql_thread_id($link_identifier = null): int |
| 889 |
{ |
| 890 |
error_clear_last(); |
| 891 |
$result = \mysql_thread_id($link_identifier); |
| 892 |
if ($result === false) { |
| 893 |
throw MysqlException::createFromPhpError(); |
| 894 |
} |
| 895 |
return $result; |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
/** |
| 900 |
* mysql_unbuffered_query sends the SQL query |
| 901 |
* query to MySQL without automatically |
| 902 |
* fetching and buffering the result rows as |
| 903 |
* mysql_query does. This saves a considerable |
| 904 |
* amount of memory with SQL queries that produce large result sets, |
| 905 |
* and you can start working on the result set immediately after the |
| 906 |
* first row has been retrieved as you don't have to wait until the |
| 907 |
* complete SQL query has been performed. To use |
| 908 |
* mysql_unbuffered_query while multiple database |
| 909 |
* connections are open, you must specify the optional parameter |
| 910 |
* link_identifier to identify which connection |
| 911 |
* you want to use. |
| 912 |
* |
| 913 |
* @param string $query The SQL query to execute. |
| 914 |
* |
| 915 |
* Data inside the query should be properly escaped. |
| 916 |
* @param resource $link_identifier The MySQL connection. If the |
| 917 |
* link identifier is not specified, the last link opened by |
| 918 |
* mysql_connect is assumed. If no such link is found, it |
| 919 |
* will try to create one as if mysql_connect had been called |
| 920 |
* with no arguments. If no connection is found or established, an |
| 921 |
* E_WARNING level error is generated. |
| 922 |
* @return resource|bool For SELECT, SHOW, DESCRIBE or EXPLAIN statements, |
| 923 |
* mysql_unbuffered_query |
| 924 |
* returns a resource on success. |
| 925 |
* |
| 926 |
* For other type of SQL statements, UPDATE, DELETE, DROP, etc, |
| 927 |
* mysql_unbuffered_query returns TRUE on success. |
| 928 |
* @throws MysqlException |
| 929 |
* |
| 930 |
*/ |
| 931 |
function mysql_unbuffered_query(string $query, $link_identifier = null) |
| 932 |
{ |
| 933 |
error_clear_last(); |
| 934 |
$result = \mysql_unbuffered_query($query, $link_identifier); |
| 935 |
if ($result === false) { |
| 936 |
throw MysqlException::createFromPhpError(); |
| 937 |
} |
| 938 |
return $result; |
| 939 |
} |
| 940 |
|