Oihana Php Mysql

MysqlUserTrait uses trait:short, \oihana\models\pdo\PDOTrait

Provides methods to manage MySQL users using PDO.

Includes operations for creating, renaming, deleting, and checking the existence of users.

Requires a connected PDO instance and uses MysqlAssertionsTrait for input validation.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

createUser()  : bool
Creates a new MySQL user with the given username, host, and password.
dropUser()  : bool
Drops a MySQL user if it exists.
getUserInfo()  : array<string|int, mixed>|null
Retrieves complete information about a MySQL user.
listUsers()  : array<string|int, mixed>
Returns a list of MySQL users with their associated hosts.
renameUser()  : bool
Renames an existing MySQL user.
userExists()  : bool
Checks if a MySQL user exists.
assertHost()  : void
Validates a MySQL host string.
assertIdentifier()  : void
Validates a MySQL identifier such as a database name, user name, or table name.

Methods

createUser()

Creates a new MySQL user with the given username, host, and password.

public createUser(string $username[, string $host = 'localhost' ][, string $password = '' ]) : bool

If the user already exists, the operation has no effect.

Parameters
$username : string

The username to create.

$host : string = 'localhost'

The host from which the user connects (default: 'localhost').

$password : string = ''

The password for the user.

Return values
bool

True on success, false otherwise.

dropUser()

Drops a MySQL user if it exists.

public dropUser(string $username[, string $host = 'localhost' ]) : bool
Parameters
$username : string

The username to drop.

$host : string = 'localhost'

The host (default: 'localhost').

Return values
bool

True on success, false otherwise.

getUserInfo()

Retrieves complete information about a MySQL user.

public getUserInfo(string $username[, string $host = 'localhost' ][, bool $throwable = false ]) : array<string|int, mixed>|null
Parameters
$username : string

The username to get information for.

$host : string = 'localhost'

The host (default: 'localhost').

$throwable : bool = false

Indicates if the method should throw exceptions.

Return values
array<string|int, mixed>|null

Returns user information array or null if user doesn't exist. Array contains: user, host, password_expired, account_locked, password_last_changed, password_lifetime, max_connections, max_questions, max_updates, max_user_connections, plugin, authentication_string, ssl_type, ssl_cipher, x509_issuer, x509_subject, and grants.

listUsers()

Returns a list of MySQL users with their associated hosts.

public listUsers([string|null $like = null ][, bool $grouped = false ][, bool $throwable = false ]) : array<string|int, mixed>
Parameters
$like : string|null = null

Optional SQL pattern to filter users (e.g. 'wp%').

$grouped : bool = false

Whether to group hosts under each username.

$throwable : bool = false

Indicates if the method is throwable.

Return values
array<string|int, mixed>

If grouped, returns array<string, string[]> (user => [hosts]). Otherwise, returns array<int, array{user: string, host: string}>.

renameUser()

Renames an existing MySQL user.

public renameUser(string $fromUser, string $fromHost, string $toUser, string $toHost) : bool
Parameters
$fromUser : string

Current username.

$fromHost : string

Current host.

$toUser : string

New username.

$toHost : string

New host.

Return values
bool

True if the rename operation was successful, false otherwise.

userExists()

Checks if a MySQL user exists.

public userExists(string $username[, string $host = 'localhost' ]) : bool
Parameters
$username : string

Username to check.

$host : string = 'localhost'

Host (default: 'localhost').

Return values
bool

True if the user exists.

assertHost()

Validates a MySQL host string.

protected assertHost(string $host) : void

A valid host string may contain:

  • letters (a–z, A–Z)
  • digits (0–9)
  • dots (.)
  • hyphens (-)
  • underscores (_) and percent signs (%) for wildcards
Parameters
$host : string

The host name or IP to validate (e.g., 'localhost', '127.0.0.1', '%.example.com').

Tags
throws
InvalidArgumentException

If the host string contains disallowed characters.

assertIdentifier()

Validates a MySQL identifier such as a database name, user name, or table name.

protected assertIdentifier(string $name) : void

A valid identifier consists of letters (a–z, A–Z), digits (0–9), and underscores (_). This ensures safe usage in SQL queries without risk of injection or syntax errors.

Parameters
$name : string

The identifier to validate.

Tags
throws
InvalidArgumentException

If the identifier contains invalid characters.

On this page

Search results