Database abstraction class for MySQL.
| Db_mysql | Database abstraction class for MySQL. |
| Variables | |
| $query_count | Keeps track of number of queries made in this database connection. |
| $error | Contains the last SQL error. |
| $query | Contains the last query executed. |
| $isConnected | A boolean showing the status of the database connection. |
| $db | Contains a MySQL link identifier. |
| Connection Details | |
| Functions | |
| __construct | Saves the connection data for later use. |
| __destruct | Closes the connection to the MySQL server. |
| execute | Checks if a connection is made, otherwise first calls connect. |
| fetch | Fetches the data from the result set and returns it as an object. |
| fetchArray | Fetches the data from the result set and returns it as an array. |
| fetchAll | Fetches all the data from a result set and returns it as an array of results. |
| fetchRow | Combines execute and fetch to a single function for fetching single rows of data. |
| numRows | Uses mysql_num_rows on a result set to return the number of rows. |
| insert | Takes an array to generate an INSERT query, then executes it. |
| affected | Returns the number of affected rows from the last query executed. |
| connect | Connects to a MySQL server and selects a database. |
public function execute( $query, $params = )
Checks if a connection is made, otherwise first calls connect. Executes a query and returns the result (a resource or boolean).
| $query | A string with the SQL query to execute. |
| $params | An optional array of parameters to use with parameter binding. |
The result of the SQL query. Either a resource or a boolean. On a failed query, it returns false. On an SQL error, it throws a DbException with the MySQL error.
$query = $db->execute('SELECT username FROM <ezrpg>players WHERE id=?', array($player->id));$query = $db->execute('SELECT COUNT(id) AS count FROM <ezrpg>players');
public function fetch( & $result )
Fetches the data from the result set and returns it as an object.
| $result | A result set from an SQL query. |
The results from the query in an array.
$query = $db->execute('SELECT COUNT(id) AS count FROM <ezrpg>players');
$result = $db->fetch($query);
echo $result->count;
public function fetchArray( & $result )
Fetches the data from the result set and returns it as an array.
| $result | A result set from an SQL query. |
An array with the query results.
$query = $db->execute('SELECT COUNT(`id`) AS `count` FROM `<ezrpg>players`');
$result = $db->fetchArray($query);
echo $result['count'];
public function fetchAll( & $result, $return_array = false )
Fetches all the data from a result set and returns it as an array of results.
| $result | A result set from an SQL query. |
| $return_array | Boolean to return the result as arrays or objects. |
An array of arrays/objects from query results. On a failed query, returns false.
$query = $db->execute('SELECT `id` FROM `<ezrpg>players`');
$results = $db->fetchAll($query);
foreach ($results as $row)
echo $row->id;
public function fetchRow( $query, $params = )
Combines execute and fetch to a single function for fetching single rows of data.
Equivalent to using mysql_fetch_object(mysql_query($query)).
After the row is fetched, all memory associated with the result is freed with mysql_free_result().
| $query | A string with the SQL query to execute. |
| $params | An optional array of parameters to use with parameter binding. |
The results from the query in an array.
$result = $db->fetchRow('SELECT COUNT(id) AS count FROM <ezrpg>players');
echo $result->count;
public function insert( $table, $data )
Takes an array to generate an INSERT query, then executes it.
| $table | Name of the table to insert into. |
| $data | An array with keys being column names. |
ID generated by the AUTO_INCREMENT column.
$insert = Array();
$insert['username'] = 'Andy';
$insert['password'] = 'a9629b9ff4f0637362a0954224e1cd5792effb62';
$insert['email'] = 'andy@ezrpgproject.com';
$insert['registered'] = time();
$new_player = $db->insert('<ezrpg>players', $insert);
protected function connect()
Connects to a MySQL server and selects a database.
| $host | Database server |
| $username | Username to login with |
| $password | Password to login with |
| $dbname | Name of database |
True if there were no errors.
Throws a DbException if there was a connection problem.
Keeps track of number of queries made in this database connection.
public $query_count
Contains the last SQL error.
public $error
Contains the last query executed.
public $query
A boolean showing the status of the database connection.
public $isConnected
Contains a MySQL link identifier.
protected $db
Saves the connection data for later use.
public function __construct( $host = 'localhost', $username = 'root', $password = '', $dbname = '' )
Closes the connection to the MySQL server.
public function __destruct()
Checks if a connection is made, otherwise first calls connect.
public function execute( $query, $params = )
Connects to a MySQL server and selects a database.
protected function connect()
Fetches the data from the result set and returns it as an object.
public function fetch( & $result )
Fetches the data from the result set and returns it as an array.
public function fetchArray( & $result )
Fetches all the data from a result set and returns it as an array of results.
public function fetchAll( & $result, $return_array = false )
Combines execute and fetch to a single function for fetching single rows of data.
public function fetchRow( $query, $params = )
Uses mysql_num_rows on a result set to return the number of rows.
public function numRows( & $result )
Takes an array to generate an INSERT query, then executes it.
public function insert( $table, $data )
Returns the number of affected rows from the last query executed.
public function affected()