usersDataSourceMySQLi
extends usersDataSource
in package
usersDataSource class
usersDataSource provides the data access capabilities for the Users class.
To build a data access class for Users, the following 5 methods must exist. getPassword(username) getAllUsers() getARecord update
Additionally, where appropriate, the following function is useful buildTable()
This particular instance provides an interface to MySQL using the mysqli libraries.
Create an instance of this, then pass the variable to several Users calls.
Table of Contents
- $configuration : array<string|int, string>
- $dbConnection : mysqli
- __construct() : null
- constructor for an instance of the class
- buildTable() : mixed
- Convenience function to create the tables defined in $configuration
- getAllUsers() : array<string|int, string>
- retrieves all users from the database
- getARecord() : array<string|int, string>
- Get a record from the database
- getPassword() : array<string|int, string>
- Retrieves the password field from table
- getUserID() : int
- retrieves the userID from the table
- initTables() : mixed
- Convenience function to initialize tables to values
- test() : bool
- Tests that the database connection works and the table is built
- update() : mysqli_result|bool
- updates row in database with $newData
- buildQuery() : string
- Create a query to retrieve info from database
- doSQL() : mixed
- Executes an SQL statement, returning the result
- escapeString() : string
- Make string safe for MySQL
- getAField() : mixed
- Gets a single field from a table
- setDBConnection() : mysqli|false
- Make the database connection
- tableColumnName() : string
- Gets the actual database column name from the configuration file
Properties
$configuration
protected
array<string|int, string>
$configuration
= array('tables' => array('users' => array(
'table' => '_users',
// table name for user records
'id' => '_user_id',
// ID column name
'display' => array(
// fields which are displayed to select
'login',
),
'password' => array(
// These fields are stored encrypted
'pass',
),
'fields' => array('login' => array(
'dbColumn' => 'login',
// login name column name
'type' => 'varchar',
'size' => 64,
'required' => rue,
'unique' => rue,
), 'pass' => array(
'dbColumn' => 'password',
// password column name
'type' => 'varchar',
'size' => 128,
'required' => rue,
), 'admin' => array('dbColumn' => 'isAdmin', 'type' => 'boolean', 'required' => rue, 'default' => '0'), 'enabled' => array('dbColumn' => 'enabled', 'type' => 'boolean', 'required' => rue, 'default' => '1')),
)))
Contains the configuration for the class
May be modified by the calling program. Must be replicated in userDataSource class
$dbConnection
protected
mysqli
$dbConnection
= alse
Holds the mysqli database connection
Methods
__construct()
constructor for an instance of the class
public
__construct(mixed $connection[, mixed $customFields = array() ]) : null
If $dbConnection is not null, will be used for database access If $dbLoginInfo is not null, will override $dbConnection, make a new connection and use that.
If $dbDef is set, will be merged with $configuration
Parameters
- $connection : mixed
- $customFields : mixed = array()
Return values
null —buildTable()
Convenience function to create the tables defined in $configuration
public
buildTable() : mixed
Using $configuration, build the table (replacing the current one) then add $username with $password, setting as admin
Return values
mixed —getAllUsers()
retrieves all users from the database
public
getAllUsers() : array<string|int, string>
Retrieves all data for all users from table
Return values
array<string|int, string> —array of array of rows/columns
getARecord()
Get a record from the database
public
getARecord(array<string|int, string> $whereFields[, array<string|int, string> $fieldList = null ]) : array<string|int, string>
Gets a single record from the database which matches $field containing $username. If more than one record is returned, will return the first one
Parameters
- $whereFields : array<string|int, string>
-
column=>value pairs for where clause
- $fieldList : array<string|int, string> = null
-
a list of columns to return. If empty, returns all columns
Return values
array<string|int, string> —a hash containing fieldname=>value pairs from fetch_assoc
getPassword()
Retrieves the password field from table
public
getPassword(string $username) : array<string|int, string>
Note that the password is stored as a hash in the table
Parameters
- $username : string
-
username used to find record
Return values
array<string|int, string> —an array of values key/value pairs
getUserID()
retrieves the userID from the table
public
getUserID(string $username) : int
Parameters
- $username : string
Return values
int —user id
initTables()
Convenience function to initialize tables to values
public
initTables(array<string|int, string> $initValues) : mixed
Parameters
- $initValues : array<string|int, string>
-
Array of tablenames, column names and values
Return values
mixed —test()
Tests that the database connection works and the table is built
public
test() : bool
Return values
bool —True if table exists (does not verify columns)
update()
updates row in database with $newData
public
update(array<string|int, string> $newData) : mysqli_result|bool
Parameters
- $newData : array<string|int, string>
-
fieldname/value pairs to be updated in table
Return values
mysqli_result|bool —The mysqli result from a query
buildQuery()
Create a query to retrieve info from database
protected
buildQuery(mixed $whereFields[, array<string|int, string> $fieldList = null ]) : string
Builds a query to retrieve records from the database. With all parameters set to null, will retrieve all columns and records Setting $field and $toFind create a where clause, and setting $fieldList as a has (ie, 'fieldname' => 1) will limit the fields returned
Parameters
- $whereFields : mixed
- $fieldList : array<string|int, string> = null
-
a hash where the keys make a list of columns to return. If empty, returns all columns
Return values
string —A cleaned and formatted SQL Query
doSQL()
Executes an SQL statement, returning the result
protected
doSQL(mixed $query[, mixed $comment = '' ]) : mixed
This simply runs mysqli::query, and returns the value of that
Created for testing and debugging, if the second parameter is true, will NOT execute the query, but will instead display the query passed.
Parameters
- $query : mixed
- $comment : mixed = ''
Tags
Return values
mixed —escapeString()
Make string safe for MySQL
protected
escapeString(string $string) : string
If the string is completely numeric, returns it, otherwise puts single quotes around it
Parameters
- $string : string
-
The string to be fixed
Return values
string —A copy of the string, ready for SQL
getAField()
Gets a single field from a table
protected
getAField(mixed $tableName, mixed $returnColumn, mixed $fieldName, mixed $value) : mixed
Builds a query similar to select $returnColumn from $tableName where $fieldName = $value executes it, and returns the first column of the first row returned, or null if it does not exist.
Parameters
- $tableName : mixed
- $returnColumn : mixed
- $fieldName : mixed
- $value : mixed
Tags
Return values
mixed —setDBConnection()
Make the database connection
protected
setDBConnection(array<string|int, string> $parameters) : mysqli|false
Parameters
- $parameters : array<string|int, string>
-
Parameters for makeing the connection
Return values
mysqli|false —tableColumnName()
Gets the actual database column name from the configuration file
protected
tableColumnName(string $table[, string $field = '' ][, bool $fullTableColumn = false ]) : string
Since we use a lot of indirection, this is a handy function which allows us to replace something like $this->configuration['tables']['users']['fields']['name']['dbColumn'] with $this->tableColumnName( 'users', 'name' )
If called with only one parameter (the table), will return the actual database table name
Parameters
- $table : string
-
Name of Table
- $field : string = ''
-
Name of field in $table
- $fullTableColumn : bool = false
-
If set to true, will return table.column format
Return values
string —The actual name of the dbColumn in the table