MysqlDriver
The MysqlDriver.php
file in the Drivers/Database/
directory of the PHPFast framework provides methods to interact with a MySQL database. It includes methods for connecting to the database, executing queries, and managing data. Below is a detailed explanation of each method along with examples of how to use them
Configuaration
'db' => [
'db_driver' => 'mysql',
'db_host' => 'localhost',
'db_port' => 3306,
'db_username' => 'root',
'db_password' => '',
'db_database' => 'cms.vn',
'db_charset' => 'utf8mb4',
'db_collate' => 'utf8mb4_unicode_ci',
],
__construct()
__construct()
<?php
public function __construct($config) {
$this->config = $config;
$this->connect();
}
Initializes the MysqlDriver
object with the provided configuration and establishes a connection to the MySQL database.
$config
: An array containing the MySQL configuration settings
query()
query()
<?php
public function query($query, $params = []) {}
Prepares and executes an SQL query with optional parameters.
$query
: The SQL query string.$params
: An array of parameters to bind to the query (optional).return
: Result of the query (used for SELECT, INSERT, UPDATE, DELETE)
lastInsertId()
lastInsertId()
<?php
public function lastInsertId() {}
Retrieves the ID of the last inserted record.
$return
: Result of the query (used for SELECT, INSERT, UPDATE, DELETE).
count()
count()
<?php
public function count($table, $where = '', $params = []) {}
Counts the number of records in a table.
$table
: The name of the table.$where
: The WHERE clause to filter records (optional).$params
: An array of values corresponding to the parameters in the WHERE clause (optional).return
: Number of records in the table.
fetchAll()
fetchAll()
<?php
public function fetchAll($table, $where = '', $params = [], $orderBy = '', $page = 1, $limit = null) {}
Excutes a SELECT query to retrieve multiple rows.
$table
: The name of the table.$where
: The WHERE clause as a string (optional).$params
: An array of values corresponding to the parameters in the WHERE clause (optional).$orderBy
: The ORDER BY clause (optional).$page
: The current page number (optional).$limit
: The number of results to limit (optional).return
: Array containing the query results.
fetchPagination()
fetchPagination()
<?php
fetchPagination($table, $where = '', $params = [], $orderBy = '', $page = 1, $limit = null) {}
Execute a SELECT query to fetch multiple rows with pagination.
$table
: Name of the table$where
: WHERE condition as a string (optional)$params
: Array of values corresponding to the WHERE string (optional)$orderBy
: ORDER BY clause (optional)$page
: Current page number (optional)$limit
: Number of results per page (optional)return
: Query results and information about whether there is a next page.
fetchPaginationWithField()
fetchPaginationWithField()
<?php
fetchPaginationWithField($table, $fields = '*', $where = '', $params = [], $orderBy = '', $page = 1, $limit = null) {}
Execute a SELECT query to fetch multiple rows with pagination.
$table
: Name of the tablefields
: Fields to query (optional)$where
: WHERE condition as a string (optional)$params
: Array of values corresponding to the WHERE string (optional)$orderBy
: ORDER BY clause (optional)$page
: Current page number (optional)$limit
: Number of results per page (optional)return
: Query results and information about whether there is a next page.
fetchRow()
fetchRow()
<?php
public function fetchRow($table, $where = '', $params = [], $orderBy = '', $page = 1) {}
Executes a SELECT query to retrieve a single row.
$table
: The name of the table.$where
: The WHERE clause as a string.$params
: An array of values corresponding to the parameters in the WHERE clause.$orderBy
: The ORDER BY clause (optional).$page
: The current page number (optional).return
: Array containing the query result or null if no result is found.
fetchRowfetchRowField()
fetchRowfetchRowField()
<?php
public function fetchRow($table, $fields = '*', $where = '', $params = [], $orderBy = '', $page = 1) {}
Executes a SELECT query to retrieve a single row.
$table
: The name of the table.fields
: Fields to query$where
: The WHERE clause as a string.$params
: An array of values corresponding to the parameters in the WHERE clause.$orderBy
: The ORDER BY clause (optional).$page
: The current page number (optional).return
: Array containing the query result or null if no result is found
insert()
insert()
<?php
public function insert($table, $data) {}
Executes an INSERT query to add a new record.
$table
: The name of the table.$data
: An array of data to insert (in the form of 'column' => 'value').return
: Returns true if the data is successfully inserted, otherwise false.
update()
update()
<?php
public function update($table, $data, $where = '', $params = []) {}
Executes an UPDATE query to modify existing records.
$table
: The name of the table.$data
: An array of data to update (in the form of 'column' => 'value').$where
: The WHERE clause to filter records.$params
: An array of values corresponding to the parameters in the WHERE clause.return
: Returns true if the update is successful, otherwise false.
delete()
delete()
<?php
public function delete($table, $where = '', $params = []) {}
Executes a DELETE query to remove records.
$table
: The name of the table.$where
: The WHERE clause to filter records.$params
: An array of values corresponding to the parameters in the WHERE clause.return
: Returns true if the deletion is successful, otherwise false.
Summary
The MysqlDriver.php
file provides a comprehensive set of methods for interacting with a MySQL database using PDO. It supports various database operations, including querying, inserting, updating, and deleting records, as well as managing transactions and pagination. This implementation allows developers to perform database operations efficiently in their PHPFast applications.
Last updated
Was this helpful?