Oihana PHP System

MysqlUserTrait uses trait:short, trait:short

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

Constants

DEFER_ASSIGNMENT  = 'deferAssignment'
The 'deferAssignment' parameter constant.
PDO  = 'pdo'
The 'pdo' parameter constant.
SCHEMA  = 'schema'
The 'schema' parameter constant.

Properties

$deferAssignment  : bool|null
Indicates if the the constructor is called before setting properties.
$pdo  : PDO|null
The PDO reference.
$schema  : string|mixed|null
The internal schema to use in the PDO fetch processes.

Methods

bindValues()  : void
Bind named parameters to a prepared PDO statement.
createUser()  : bool
Creates a new MySQL user with the given username, host, and password.
dropUser()  : bool
Drops a MySQL user if it exists.
fetch()  : mixed|null
Execute a SELECT query and fetch a single result.
fetchAll()  : array<string|int, mixed>
Execute a SELECT query and fetch all results.
fetchColumn()  : mixed
Execute a query and return the value of a single column from the first row.
fetchColumnArray()  : array<int, string>
Fetch a list of single-column results.
initializeDefaultFetchMode()  : void
Set the default fetch mode on the statement.
initPDO()  : PDO|null
Initialize the PDO instance from a config array or dependency injection container.
isConnected()  : bool
Indicates if the PDO is connected.
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.

Constants

DEFER_ASSIGNMENT

The 'deferAssignment' parameter constant.

public mixed DEFER_ASSIGNMENT = 'deferAssignment'

PDO

The 'pdo' parameter constant.

public mixed PDO = 'pdo'

SCHEMA

The 'schema' parameter constant.

public mixed SCHEMA = 'schema'

Properties

$deferAssignment

Indicates if the the constructor is called before setting properties.

public bool|null $deferAssignment = false

Only if the schema property is defined.

$pdo

The PDO reference.

public PDO|null $pdo = null

$schema

The internal schema to use in the PDO fetch processes.

public string|mixed|null $schema = null

Methods

bindValues()

Bind named parameters to a prepared PDO statement.

public bindValues(PDOStatement $statement[, array<string|int, mixed> $bindVars = [] ]) : void
Parameters
$statement : PDOStatement

The PDO statement.

$bindVars : array<string|int, mixed> = []

Associative array of bindings. Supports:

  • ['id' => 5]
  • ['id' => [5, PDO::PARAM_INT]]

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.

fetch()

Execute a SELECT query and fetch a single result.

public fetch(string $query[, array<string|int, mixed> $bindVars = [] ]) : mixed|null

The result is returned as an object or as a mapped schema class if defined. Alteration is applied via AlterDocumentTrait.

Parameters
$query : string

The SQL query to execute.

$bindVars : array<string|int, mixed> = []

Optional bindings for the query.

Tags
throws
ContainerExceptionInterface
throws
NotFoundExceptionInterface
Return values
mixed|null

The result object or null if not found.

fetchAll()

Execute a SELECT query and fetch all results.

public fetchAll(string $query[, array<string|int, mixed> $bindVars = [] ]) : array<string|int, mixed>

Results are returned as an array of associative arrays or schema instances. Alteration is applied via AlterDocumentTrait.

Parameters
$query : string

The SQL query to execute.

$bindVars : array<string|int, mixed> = []

Optional bindings for the query.

Tags
throws
ContainerExceptionInterface
throws
NotFoundExceptionInterface
Return values
array<string|int, mixed>

An array of results.

fetchColumn()

Execute a query and return the value of a single column from the first row.

public fetchColumn(string $query[, array<string|int, mixed> $bindVars = [] ][, int $column = 0 ]) : mixed
Parameters
$query : string

The SQL query to execute.

$bindVars : array<string|int, mixed> = []

Optional bindings for the query.

$column : int = 0

Column index (0-based) to return from the first row.

Return values
mixed

The column value or 0 if the query fails.

fetchColumnArray()

Fetch a list of single-column results.

public fetchColumnArray(string $query[, array<string|int, mixed> $bindVars = [] ]) : array<int, string>
Parameters
$query : string

The SQL query to execute.

$bindVars : array<string|int, mixed> = []

Optional bindings for the query.

Return values
array<int, string>

initializeDefaultFetchMode()

Set the default fetch mode on the statement.

public initializeDefaultFetchMode(PDOStatement $statement) : void

Uses FETCH_ASSOC by default or FETCH_CLASS (with optional FETCH_PROPS_LATE) if a schema class is defined and exists.

Parameters
$statement : PDOStatement

The PDO statement to configure.

initPDO()

Initialize the PDO instance from a config array or dependency injection container.

public initPDO([array<string|int, mixed> $init = [] ][, Container|null $container = null ]) : PDO|null
Parameters
$init : array<string|int, mixed> = []

Configuration array. Expects Param::PDO as key.

$container : Container|null = null

Optional DI container to resolve the PDO service.

Tags
throws
ContainerExceptionInterface
throws
NotFoundExceptionInterface
Return values
PDO|null

The resolved PDO instance or null.

isConnected()

Indicates if the PDO is connected.

public isConnected() : bool
Return values
bool

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