PHP Luminova: Database Connection Drivers and Interface Overview
Database Driver Interface: Facilitating Database Connectivity, Query Execution, and Result Handling. Explore our Database Example to Kickstart Your Integration.
Luminova database drivers implement the core interface for executing queries, preparing statements, and fetching results. To begin working with them, see the Database Examples.
Usage
The recommended way to manage database connections in Luminova is through the Database Connection Class. This allows you to switch between drivers using the .env
configuration database.connection
without changing any code.
use Luminova\Database\Connection;
$enableConnectionPool = true; // Enable connection pool
$maxConnectionPools = 5; // Max connection pools
$autoConnect = true; // Automatically open connection
$conn = new Connection($enableConnectionPool, $maxConnectionPools, $autoConnect);
Manual Driver Initialization
To initialize a specific driver manually with your application's database configuration:
MySQLi-based driver
use Luminova\Database\Drivers\MysqliDriver;
use App\Config\Database;
$mysqli = new MysqliDriver(new Database([
// connection configuration options...
]));
PDO-based driver
use Luminova\Database\Drivers\PdoDriver;
use App\Config\Database;
$pdo = new PdoDriver(new Database([
// connection configuration options...
]));
Note: Manual driver usage does not support connection pooling, fallback connections, or retry attempts.
Class Definition
Namespaces:
- MySQL Driver:
Luminova\Database\Drivers\MysqliDriver
- PDO Driver:
Luminova\Database\Drivers\PdoDriver
- MySQL Driver:
- Implements: Luminova\Interface\DatabaseInterface
Methods
constructoror
Initialize database driver with application database configurations.
public __constructor(Luminova\Core\CoreDatabase $config)
Parameters:
Parameter | Type | Description |
---|---|---|
$config | Luminova\Core\CoreDatabase | The database connection configuration object. |
Throws:
- Luminova\Exceptions\DatabaseException - If the database connection fails.
isConnected
Check if the database is connection is already established.
public isConnected(): bool
Return Value:
bool
- Returns true if connected, false otherwise.
getDriver
Get database connection driver name in use (e.g sqlite
, mysql
, cubrid
etc...).
public getDriver(): ?string
Return Value:
string|null
- Return driver name if connection is open, otherwise null.
getConfig
Retrieve a specific database configuration property.
This method allows fetching configuration values related to the database connection.If the requested property does not exist, null
is returned.
Available Properties:
- connection
(string)
: The connection method, typically'pdo'
or other (default:'pdo'
). - pdo_version
(string)
: The PDO driver version to use (e.g.,'mysql'
,'sqlite'
, etc.) (default:'mysql'
). - charset
(string)
: The character encoding for the connection (default:'utf8mb4'
). - sqlite_path
(string|null)
: Path to the SQLite database file (default:null
). - production
(bool)
: Indicates if the connection is in a production environment (default:false
). - database
(string)
: The name of the selected database (default:''
). - persistent
(bool)
: Whether to use persistent connections (default:true
). - socket
(bool)
: Whether to use a Unix socket instead of TCP/IP (default:false
). - socket_path
(string)
: The Unix socket path ifsocket
is enabled (default:''
). - emulate_preparse
(bool)
: Enables query emulation before execution (default:false
).
public getConfig(string $property): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$property | bool | The name of the configuration property. |
Return Value:
mixed
- Returns the property value if it exists, otherwise null
.
raw
Get the original database connection object of PDO
or mysqli
database.
public raw(): ?Luminova\Interface\ConnInterface
Return Value:
\Luminova\Interface\ConnInterface|null
- Returns the connection instance if connected, otherwise null.
getStatement
Retrieve Mysqli
or PDO
prepared statement of the last executed query.
public getStatement(): PDOStatement|mysqli_stmt|null
Return Value:
PDOStatement|mysqli_stmt|null
- Retrieve mysqli
or PDO
prepared statement of the last executed query.
setDebug
Sets the debug mode.
public setDebug(bool $debug): self
Return Value:
self
- Returns the current instance.
Parameters:
Parameter | Type | Description |
---|---|---|
$debug | bool | Enable or disable debug mode. |
error
Get the error information for the last executed statement.
public error(): string
Return Value:
string
- Returns the error information as a string.
errors
Get all error information.
public errors(): array
Return Value:
array
- Returns an array of error information.
dumpDebug
Dump debug information for the last executed statement.
public dumpDebug(): bool
Return Value:
bool
- Returns true if debug information is dumped, otherwise false.
info
Get information about the last executed statement.
public info(): array
Return Value:
array
- Returns an array of statement execution information.
prepare
Prepare a statement for execution.
public prepare(string $query): self
Return Value:
self
- Returns the current instance.
Parameters:
Parameter | Type | Description |
---|---|---|
$query | string | The SQL query string to execute. |
query
Execute a query without using placeholders.
public query(string $query): self
Return Value:
self
- Returns the current instance.
Parameters:
Parameter | Type | Description |
---|---|---|
$query | string | The SQL query string to execute. |
exec
Execute an SQL statement and return the number of affected rows.
public exec(string $query): int
Return Value:
int
- Returns the number of affected rows.
Parameters:
Parameter | Type | Description |
---|---|---|
$query | string | The SQL statement to execute. |
beginTransaction
Begins a transaction with optional read-only isolation level and SAVEPOINT
for PDO
.
public beginTransaction(int $flags = 0, ?string $name = null): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$flags | int | Optional flags to set transaction properties. (Default: 0 ).No predefined flags for PDO , specify 4 to create read-only isolation. |
$name | string|null | Optional name. If provided in PDO , SAVEPOINT will be created with name instead. |
Return Value:
bool
- Return true if transaction started successfully, otherwise false.
Throws:
- Luminova\Exceptions\DatabaseException - Throws exception on
PDO
if failure to set transaction isolation level or createsavepoint
.
Note:
If
$flags
is set to4
inPDO
, which is equivalent toMYSQLI_TRANS_START_READ_ONLY
, a read-only isolation level will be established. If the transaction starts successfully, it will return true.If
$name
is specified inPDO
, aSAVEPOINT
will be created. If the savepoint creation is successful, the transaction will return true.
commit
Commits a transaction.
public commit(int $flags = 0, ?string $name = null): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$flags | int | Optional flags to set transaction properties. (Default: 0 ).Only supported in MySQLi . |
$name | string|null | Optional name. Only supported in MySQLi . |
Return Value:
bool
- Returns true if the transaction was successfully committed, otherwise false.
rollback
Rolls back the current transaction or to a specific name while in PDO
uses SAVEPOINT
.
public rollback(int $flags = 0, ?string $name = null): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$flags | int | Optional flags to set transaction properties. (Default: 0 ).Only supported in MySQLi . |
$name | string|null | Optional name. If provided in PDO , rolls back to the SAVEPOINT named |
Return Value:
bool
- Return true if rolled back was successful, otherwise false.
Throws:
- Luminova\Exceptions\DatabaseException - Throws exception on
PDO
if failure to createSAVEPOINT
.
inTransaction
Determine if there is any active transaction.
public inTransaction(): bool
Return Value:
bool
- Return true if any active transaction, otherwise false.
getType
Returns the appropriate parameter type based on the value.
public static getType(mixed $value): string|int|null
Parameters:
Parameter | Type | Description |
---|---|---|
$value | mixed | The parameter value. |
Return Value:
string|int|null
- The parameter type.
bind
Binds a value to a parameter.
public bind(string $param, mixed $value, ?int $type = null): self
Parameters:
Parameter | Type | Description |
---|---|---|
$param | string | The parameter identifier. |
$value | mixed | The parameter value. |
$type | int|null | The parameter type. |
Return Value:
self
- Returns the current instance.
param
Binds a variable to a parameter.
public param(string $param, mixed &$value, ?int $type = null): self
Parameters:
Parameter | Type | Description |
---|---|---|
$param | string | The parameter identifier. |
&$value | mixed | The parameter value passed by reference. |
$type | int|null | The parameter type. |
Return Value:
self
- Returns the current instance.
execute
Executes the prepared statement.
public execute(?array $params = null): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$params | array|null | An optional list array to bound parameters while executing statement. Each value is treated as a string. |
Return Value:
bool
- Returns true on success, false on failure.
Throws:
- Luminova\Exceptions\DatabaseException - Query statment encounter error.
ok
Check if query execution is completed successfully.
public function ok(): bool;
Return Value:
bool
- Returns true on success, false on failure.
rowCount
Returns the number of rows affected by the last statement execution.
public rowCount(): int
Return Value:
int|bool
- Returns the number of affected rows.
getNext
Fetches a next single row as an object or array.
public getNext(string $type = 'object'): array|object|false
Parameters:
Parameter | Type | Description |
---|---|---|
$type | string | The type of result to return ('object' or 'array'). |
Return Value:
array|object|false
- The result object based on mode, otherwise return false on failure.
getAll
Fetches all rows as an array or objects.
public getAll(string $type = 'object'): array|object|false
Parameters:
Parameter | Type | Description |
---|---|---|
$type | string | The type of result to return ('object' or 'array'). |
Return Value:
array|object|false
- The result object based on mode, otherwise return false on failure.
getInt
Fetches selected rows as a 2D array of integers.
public getInt(): array|false
Return Value:
array|bool
- 2D array of integers else return false on failure.
getCount
Fetches total count of selected rows as integer.
public getCount(): int
Return Value:
int
- Returns the total count as an integer, or 0
on failure.
getColumns
Get columns
public getColumns(int $mode = FETCH_COLUMN): array
Parameters:
Parameter | Type | Description |
---|---|---|
$mode | int | Fetch column mode [FETCH_COLUMN or FETCH_COLUMN_ASSOC] |
Return Value:
array
- Return and associative array or indexed array of columns depending on mode.
fetch
Fetch result with a specific type and mode
public fetch(string $type = 'all', int $mode = FETCH_OBJ): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$type | string | The type of fetch method ('all' or 'next'). |
$mode | int | Controls the contents of the returned. |
Return Value:
mixed
- Return selected row item false on failure.
fetchObject
Fetches the result set as an object of the specified class or stdClass.
public fetchObject(string|null $class = 'stdClass', array $arguments = []): object|false
Parameters:
Parameter | Type | Description |
---|---|---|
$class | string|null | The name of the class to instantiate, defaults to stdClass. |
$arguments | array | Additional arguments to pass to the class constructor. |
Return Value:
object|false
- Returns the fetched object or false if no more rows are available or an error occurs.
getLastInsertId
Returns the ID of the last inserted record or sequence value.
public getLastInsertId(): string
Return Value:
string
- The last insert ID.
getResult
Retrieves a result item based on the specified mode and return type.
This method allows flexible result retrieval using a single interface, depending on the mode and return type provided.
Available Modes:
RETURN_NEXT
: Fetch the next row (same as$db->getNext($return)
).RETURN_2D_NUM
: Fetch a 2D numeric array (same as$db->getInt()
).RETURN_INT
: Fetch a single integer value (same as$db->getCount()
).RETURN_ID
: Fetch the last insert ID (same as$db->getLastInsertId()
).RETURN_COUNT
: Fetch the count of affected rows (same as$db->rowCount()
).RETURN_COLUMN
: Fetch specific columns (same as$db->getColumns()
).RETURN_ALL
: Fetch all rows (same as$db->getAll($return)
).RETURN_STMT
: Return the statement object itself (same as$db->getStatement()
).RETURN_RESULT
: Return the query result in mysqli or statement in PDO.
public getResult(int $mode = RETURN_ALL string $return = 'object'): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$mode | int | The mode of the result to return (e.g., RETURN_* constants). See database return modes . |
$return | string | The return type when applicable (e.g., array or object ).Used only with RETURN_NEXT and RETURN_ALL modes. |
Return Value:
mixed
- Return the result based on the specified mode and return type.
free
Frees up the statement cursor and sets the statement object to null.
public free(): void
close
Frees up the statement cursor and close database connection.
public close(): void