| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\MsqlException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Returns number of affected rows by the last SELECT, UPDATE or DELETE |
| 9 |
* query associated with result. |
| 10 |
* |
| 11 |
* @param resource $result The result resource that |
| 12 |
* is being evaluated. This result comes from a call to |
| 13 |
* msql_query. |
| 14 |
* @return int Returns the number of affected rows on success. |
| 15 |
* @throws MsqlException |
| 16 |
* |
| 17 |
*/ |
| 18 |
function msql_affected_rows($result): int |
| 19 |
{ |
| 20 |
error_clear_last(); |
| 21 |
$result = \msql_affected_rows($result); |
| 22 |
if ($result === false) { |
| 23 |
throw MsqlException::createFromPhpError(); |
| 24 |
} |
| 25 |
return $result; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* msql_close closes the non-persistent connection to |
| 31 |
* the mSQL server that's associated with the specified link identifier. |
| 32 |
* |
| 33 |
* Using msql_close isn't usually necessary, as |
| 34 |
* non-persistent open links are automatically closed at the end of the |
| 35 |
* script's execution. See also freeing resources. |
| 36 |
* |
| 37 |
* @param resource|null $link_identifier The mSQL connection. |
| 38 |
* If not specified, the last link opened by msql_connect |
| 39 |
* is assumed. If no such link is found, the function will try to establish a |
| 40 |
* link as if msql_connect was called, and use it. |
| 41 |
* @throws MsqlException |
| 42 |
* |
| 43 |
*/ |
| 44 |
function msql_close($link_identifier = null): void |
| 45 |
{ |
| 46 |
error_clear_last(); |
| 47 |
if ($link_identifier !== null) { |
| 48 |
$result = \msql_close($link_identifier); |
| 49 |
} else { |
| 50 |
$result = \msql_close(); |
| 51 |
} |
| 52 |
if ($result === false) { |
| 53 |
throw MsqlException::createFromPhpError(); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
* msql_connect establishes a connection to a mSQL |
| 60 |
* server. |
| 61 |
* |
| 62 |
* If a second call is made to msql_connect with |
| 63 |
* the same arguments, no new link will be established, but instead, the |
| 64 |
* link identifier of the already opened link will be returned. |
| 65 |
* |
| 66 |
* The link to the server will be closed as soon as the execution of the |
| 67 |
* script ends, unless it's closed earlier by explicitly calling |
| 68 |
* msql_close. |
| 69 |
* |
| 70 |
* @param string|null $hostname The hostname can also include a port number. e.g. |
| 71 |
* hostname,port. |
| 72 |
* |
| 73 |
* If not specified, the connection is established by the means of a Unix |
| 74 |
* domain socket, being then more efficient then a localhost TCP socket |
| 75 |
* connection. |
| 76 |
* |
| 77 |
* While this function will accept a colon (:) as a |
| 78 |
* host/port separator, a comma (,) is the preferred |
| 79 |
* method. |
| 80 |
* |
| 81 |
* @return resource Returns a positive mSQL link identifier on success. |
| 82 |
* @throws MsqlException |
| 83 |
* |
| 84 |
*/ |
| 85 |
function msql_connect(?string $hostname = null) |
| 86 |
{ |
| 87 |
error_clear_last(); |
| 88 |
if ($hostname !== null) { |
| 89 |
$result = \msql_connect($hostname); |
| 90 |
} else { |
| 91 |
$result = \msql_connect(); |
| 92 |
} |
| 93 |
if ($result === false) { |
| 94 |
throw MsqlException::createFromPhpError(); |
| 95 |
} |
| 96 |
return $result; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* msql_create_db attempts to create a new database on |
| 102 |
* the mSQL server. |
| 103 |
* |
| 104 |
* @param string $database_name The name of the mSQL database. |
| 105 |
* @param resource|null $link_identifier The mSQL connection. |
| 106 |
* If not specified, the last link opened by msql_connect |
| 107 |
* is assumed. If no such link is found, the function will try to establish a |
| 108 |
* link as if msql_connect was called, and use it. |
| 109 |
* @throws MsqlException |
| 110 |
* |
| 111 |
*/ |
| 112 |
function msql_create_db(string $database_name, $link_identifier = null): void |
| 113 |
{ |
| 114 |
error_clear_last(); |
| 115 |
if ($link_identifier !== null) { |
| 116 |
$result = \msql_create_db($database_name, $link_identifier); |
| 117 |
} else { |
| 118 |
$result = \msql_create_db($database_name); |
| 119 |
} |
| 120 |
if ($result === false) { |
| 121 |
throw MsqlException::createFromPhpError(); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* msql_data_seek moves the internal row |
| 128 |
* pointer of the mSQL result associated with the specified query |
| 129 |
* identifier to point to the specified row number. The next call |
| 130 |
* to msql_fetch_row would return that |
| 131 |
* row. |
| 132 |
* |
| 133 |
* @param resource $result The result resource that |
| 134 |
* is being evaluated. This result comes from a call to |
| 135 |
* msql_query. |
| 136 |
* @param int $row_number The seeked row number. |
| 137 |
* @throws MsqlException |
| 138 |
* |
| 139 |
*/ |
| 140 |
function msql_data_seek($result, int $row_number): void |
| 141 |
{ |
| 142 |
error_clear_last(); |
| 143 |
$result = \msql_data_seek($result, $row_number); |
| 144 |
if ($result === false) { |
| 145 |
throw MsqlException::createFromPhpError(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* msql_db_query selects a database and executes a query |
| 152 |
* on it. |
| 153 |
* |
| 154 |
* @param string $database The name of the mSQL database. |
| 155 |
* @param string $query The SQL query. |
| 156 |
* @param resource|null $link_identifier The mSQL connection. |
| 157 |
* If not specified, the last link opened by msql_connect |
| 158 |
* is assumed. If no such link is found, the function will try to establish a |
| 159 |
* link as if msql_connect was called, and use it. |
| 160 |
* @return resource Returns a positive mSQL query identifier to the query result. |
| 161 |
* @throws MsqlException |
| 162 |
* |
| 163 |
*/ |
| 164 |
function msql_db_query(string $database, string $query, $link_identifier = null) |
| 165 |
{ |
| 166 |
error_clear_last(); |
| 167 |
if ($link_identifier !== null) { |
| 168 |
$result = \msql_db_query($database, $query, $link_identifier); |
| 169 |
} else { |
| 170 |
$result = \msql_db_query($database, $query); |
| 171 |
} |
| 172 |
if ($result === false) { |
| 173 |
throw MsqlException::createFromPhpError(); |
| 174 |
} |
| 175 |
return $result; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* msql_drop_db attempts to drop (remove) a database |
| 181 |
* from the mSQL server. |
| 182 |
* |
| 183 |
* @param string $database_name The name of the database. |
| 184 |
* @param resource|null $link_identifier The mSQL connection. |
| 185 |
* If not specified, the last link opened by msql_connect |
| 186 |
* is assumed. If no such link is found, the function will try to establish a |
| 187 |
* link as if msql_connect was called, and use it. |
| 188 |
* @throws MsqlException |
| 189 |
* |
| 190 |
*/ |
| 191 |
function msql_drop_db(string $database_name, $link_identifier = null): void |
| 192 |
{ |
| 193 |
error_clear_last(); |
| 194 |
if ($link_identifier !== null) { |
| 195 |
$result = \msql_drop_db($database_name, $link_identifier); |
| 196 |
} else { |
| 197 |
$result = \msql_drop_db($database_name); |
| 198 |
} |
| 199 |
if ($result === false) { |
| 200 |
throw MsqlException::createFromPhpError(); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
/** |
| 206 |
* msql_field_len returns the length of the specified |
| 207 |
* field. |
| 208 |
* |
| 209 |
* @param resource $result The result resource that |
| 210 |
* is being evaluated. This result comes from a call to |
| 211 |
* msql_query. |
| 212 |
* @param int $field_offset The numerical field offset. The |
| 213 |
* field_offset starts at 1. |
| 214 |
* @return int Returns the length of the specified field. |
| 215 |
* @throws MsqlException |
| 216 |
* |
| 217 |
*/ |
| 218 |
function msql_field_len($result, int $field_offset): int |
| 219 |
{ |
| 220 |
error_clear_last(); |
| 221 |
$result = \msql_field_len($result, $field_offset); |
| 222 |
if ($result === false) { |
| 223 |
throw MsqlException::createFromPhpError(); |
| 224 |
} |
| 225 |
return $result; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
/** |
| 230 |
* msql_field_name gets the name of the specified field |
| 231 |
* index. |
| 232 |
* |
| 233 |
* @param resource $result The result resource that |
| 234 |
* is being evaluated. This result comes from a call to |
| 235 |
* msql_query. |
| 236 |
* @param int $field_offset The numerical field offset. The |
| 237 |
* field_offset starts at 1. |
| 238 |
* @return string The name of the field. |
| 239 |
* @throws MsqlException |
| 240 |
* |
| 241 |
*/ |
| 242 |
function msql_field_name($result, int $field_offset): string |
| 243 |
{ |
| 244 |
error_clear_last(); |
| 245 |
$result = \msql_field_name($result, $field_offset); |
| 246 |
if ($result === false) { |
| 247 |
throw MsqlException::createFromPhpError(); |
| 248 |
} |
| 249 |
return $result; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
/** |
| 254 |
* Seeks to the specified field offset. If the next call to |
| 255 |
* msql_fetch_field won't include a field offset, this |
| 256 |
* field would be returned. |
| 257 |
* |
| 258 |
* @param resource $result The result resource that |
| 259 |
* is being evaluated. This result comes from a call to |
| 260 |
* msql_query. |
| 261 |
* @param int $field_offset The numerical field offset. The |
| 262 |
* field_offset starts at 1. |
| 263 |
* @throws MsqlException |
| 264 |
* |
| 265 |
*/ |
| 266 |
function msql_field_seek($result, int $field_offset): void |
| 267 |
{ |
| 268 |
error_clear_last(); |
| 269 |
$result = \msql_field_seek($result, $field_offset); |
| 270 |
if ($result === false) { |
| 271 |
throw MsqlException::createFromPhpError(); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
/** |
| 277 |
* Returns the name of the table that the specified field is in. |
| 278 |
* |
| 279 |
* @param resource $result The result resource that |
| 280 |
* is being evaluated. This result comes from a call to |
| 281 |
* msql_query. |
| 282 |
* @param int $field_offset The numerical field offset. The |
| 283 |
* field_offset starts at 1. |
| 284 |
* @return int The name of the table on success. |
| 285 |
* @throws MsqlException |
| 286 |
* |
| 287 |
*/ |
| 288 |
function msql_field_table($result, int $field_offset): int |
| 289 |
{ |
| 290 |
error_clear_last(); |
| 291 |
$result = \msql_field_table($result, $field_offset); |
| 292 |
if ($result === false) { |
| 293 |
throw MsqlException::createFromPhpError(); |
| 294 |
} |
| 295 |
return $result; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
/** |
| 300 |
* msql_field_type gets the type of the specified field |
| 301 |
* index. |
| 302 |
* |
| 303 |
* @param resource $result The result resource that |
| 304 |
* is being evaluated. This result comes from a call to |
| 305 |
* msql_query. |
| 306 |
* @param int $field_offset The numerical field offset. The |
| 307 |
* field_offset starts at 1. |
| 308 |
* @return string The type of the field. One of int, |
| 309 |
* char, real, ident, |
| 310 |
* null or unknown. This functions will |
| 311 |
* return FALSE on failure. |
| 312 |
* @throws MsqlException |
| 313 |
* |
| 314 |
*/ |
| 315 |
function msql_field_type($result, int $field_offset): string |
| 316 |
{ |
| 317 |
error_clear_last(); |
| 318 |
$result = \msql_field_type($result, $field_offset); |
| 319 |
if ($result === false) { |
| 320 |
throw MsqlException::createFromPhpError(); |
| 321 |
} |
| 322 |
return $result; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
/** |
| 327 |
* msql_free_result frees the memory associated |
| 328 |
* with query_identifier. When PHP completes a |
| 329 |
* request, this memory is freed automatically, so you only need to |
| 330 |
* call this function when you want to make sure you don't use too |
| 331 |
* much memory while the script is running. |
| 332 |
* |
| 333 |
* @param resource $result The result resource that |
| 334 |
* is being evaluated. This result comes from a call to |
| 335 |
* msql_query. |
| 336 |
* @throws MsqlException |
| 337 |
* |
| 338 |
*/ |
| 339 |
function msql_free_result($result): void |
| 340 |
{ |
| 341 |
error_clear_last(); |
| 342 |
$result = \msql_free_result($result); |
| 343 |
if ($result === false) { |
| 344 |
throw MsqlException::createFromPhpError(); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
/** |
| 350 |
* msql_pconnect acts very much like |
| 351 |
* msql_connect with two major differences. |
| 352 |
* |
| 353 |
* First, when connecting, the function would first try to find a |
| 354 |
* (persistent) link that's already open with the same host. |
| 355 |
* If one is found, an identifier for it will be returned instead of opening |
| 356 |
* a new connection. |
| 357 |
* |
| 358 |
* Second, the connection to the SQL server will not be closed when the |
| 359 |
* execution of the script ends. Instead, the link will remain open for |
| 360 |
* future use (msql_close will not close links |
| 361 |
* established by this function). |
| 362 |
* |
| 363 |
* @param string|null $hostname The hostname can also include a port number. e.g. |
| 364 |
* hostname,port. |
| 365 |
* |
| 366 |
* If not specified, the connection is established by the means of a Unix |
| 367 |
* domain socket, being more efficient than a localhost TCP socket |
| 368 |
* connection. |
| 369 |
* |
| 370 |
* @return resource Returns a positive mSQL link identifier on success. |
| 371 |
* @throws MsqlException |
| 372 |
* |
| 373 |
*/ |
| 374 |
function msql_pconnect(?string $hostname = null) |
| 375 |
{ |
| 376 |
error_clear_last(); |
| 377 |
if ($hostname !== null) { |
| 378 |
$result = \msql_pconnect($hostname); |
| 379 |
} else { |
| 380 |
$result = \msql_pconnect(); |
| 381 |
} |
| 382 |
if ($result === false) { |
| 383 |
throw MsqlException::createFromPhpError(); |
| 384 |
} |
| 385 |
return $result; |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
/** |
| 390 |
* msql_query sends a query to the currently active |
| 391 |
* database on the server that's associated with the specified link |
| 392 |
* identifier. |
| 393 |
* |
| 394 |
* @param string $query The SQL query. |
| 395 |
* @param resource|null $link_identifier The mSQL connection. |
| 396 |
* If not specified, the last link opened by msql_connect |
| 397 |
* is assumed. If no such link is found, the function will try to establish a |
| 398 |
* link as if msql_connect was called, and use it. |
| 399 |
* @return resource Returns a positive mSQL query identifier on success. |
| 400 |
* @throws MsqlException |
| 401 |
* |
| 402 |
*/ |
| 403 |
function msql_query(string $query, $link_identifier = null) |
| 404 |
{ |
| 405 |
error_clear_last(); |
| 406 |
if ($link_identifier !== null) { |
| 407 |
$result = \msql_query($query, $link_identifier); |
| 408 |
} else { |
| 409 |
$result = \msql_query($query); |
| 410 |
} |
| 411 |
if ($result === false) { |
| 412 |
throw MsqlException::createFromPhpError(); |
| 413 |
} |
| 414 |
return $result; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
/** |
| 419 |
* msql_select_db sets the current active database on |
| 420 |
* the server that's associated with the specified |
| 421 |
* link_identifier. |
| 422 |
* |
| 423 |
* Subsequent calls to msql_query will be made on the |
| 424 |
* active database. |
| 425 |
* |
| 426 |
* @param string $database_name The database name. |
| 427 |
* @param resource|null $link_identifier The mSQL connection. |
| 428 |
* If not specified, the last link opened by msql_connect |
| 429 |
* is assumed. If no such link is found, the function will try to establish a |
| 430 |
* link as if msql_connect was called, and use it. |
| 431 |
* @throws MsqlException |
| 432 |
* |
| 433 |
*/ |
| 434 |
function msql_select_db(string $database_name, $link_identifier = null): void |
| 435 |
{ |
| 436 |
error_clear_last(); |
| 437 |
if ($link_identifier !== null) { |
| 438 |
$result = \msql_select_db($database_name, $link_identifier); |
| 439 |
} else { |
| 440 |
$result = \msql_select_db($database_name); |
| 441 |
} |
| 442 |
if ($result === false) { |
| 443 |
throw MsqlException::createFromPhpError(); |
| 444 |
} |
| 445 |
} |
| 446 |
|