Db_mysql

Database abstraction class for MySQL.

See Also

Summary
Db_mysqlDatabase abstraction class for MySQL.
Variables
$query_countKeeps track of number of queries made in this database connection.
$errorContains the last SQL error.
$queryContains the last query executed.
$isConnectedA boolean showing the status of the database connection.
$dbContains a MySQL link identifier.
Connection Details
Functions
__constructSaves the connection data for later use.
__destructCloses the connection to the MySQL server.
executeChecks if a connection is made, otherwise first calls connect.
fetchFetches the data from the result set and returns it as an object.
fetchArrayFetches the data from the result set and returns it as an array.
fetchAllFetches all the data from a result set and returns it as an array of results.
fetchRowCombines execute and fetch to a single function for fetching single rows of data.
numRowsUses mysql_num_rows on a result set to return the number of rows.
insertTakes an array to generate an INSERT query, then executes it.
affectedReturns the number of affected rows from the last query executed.
connectConnects to a MySQL server and selects a database.

Variables

$query_count

public $query_count

Keeps track of number of queries made in this database connection.

$error

public $error

Contains the last SQL error.

$query

public $query

Contains the last query executed.

$isConnected

public $isConnected

A boolean showing the status of the database connection.

$db

protected $db

Contains a MySQL link identifier.

Connection Details

$hostHost name of database server.
$dbnameName of the database.
$usernameUsername to connect with.
$passwordPassword to connect with.

Functions

__construct

public function __construct($host = 'localhost',
$username = 'root',
$password = '',
$dbname = '')

Saves the connection data for later use.  Does not start a connection yet.

Parameters

$hostDatabase server
$usernameUsername to login with
$passwordPassword to login with
$dbnameName of database

__destruct

public function __destruct()

Closes the connection to the MySQL server.

execute

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).

Parameters

$queryA string with the SQL query to execute.
$paramsAn optional array of parameters to use with parameter binding.

Returns

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.

Example Usage

$query = $db->execute('SELECT username FROM <ezrpg>players WHERE id=?', array($player->id));
$query = $db->execute('SELECT COUNT(id) AS count FROM <ezrpg>players');

See Also

fetch

public function fetch(&$result)

Fetches the data from the result set and returns it as an object.

Parameters

$resultA result set from an SQL query.

Returns

The results from the query in an array.

Example Usage

$query = $db->execute('SELECT COUNT(id) AS count FROM <ezrpg>players');
$result = $db->fetch($query);
echo $result->count;

See Also

fetchArray

public function fetchArray(&$result)

Fetches the data from the result set and returns it as an array.

Parameters

$resultA result set from an SQL query.

Returns

An array with the query results.

Example Usage

$query = $db->execute('SELECT COUNT(`id`) AS `count` FROM `<ezrpg>players`');
$result = $db->fetchArray($query);
echo $result['count'];

See Also

fetchAll

public function fetchAll(&$result,  
$return_array =  false)

Fetches all the data from a result set and returns it as an array of results.

Parameters

$resultA result set from an SQL query.
$return_arrayBoolean to return the result as arrays or objects.

Returns

An array of arrays/objects from query results.  On a failed query, returns false.

Example Usage

$query = $db->execute('SELECT `id` FROM `<ezrpg>players`');
$results = $db->fetchAll($query);
foreach ($results as $row)
  echo $row->id;

See Also

fetchRow

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().

Parameters

$queryA string with the SQL query to execute.
$paramsAn optional array of parameters to use with parameter binding.

Returns

The results from the query in an array.

Example Usage

$result = $db->fetchRow('SELECT COUNT(id) AS count FROM <ezrpg>players');
echo $result->count;

See Also

numRows

public function numRows(&$result)

Uses mysql_num_rows on a result set to return the number of rows.

Parameters

$resultA result set from an SQL query.

Returns

The number of rows in the result set.

insert

public function insert($table,
$data)

Takes an array to generate an INSERT query, then executes it.

Parameters

$tableName of the table to insert into.
$dataAn array with keys being column names.

Returns

ID generated by the AUTO_INCREMENT column.

Example Usage

$insert = Array();
$insert['username'] = 'Andy';
$insert['password'] = 'a9629b9ff4f0637362a0954224e1cd5792effb62';
$insert['email'] = 'andy@ezrpgproject.com';
$insert['registered'] = time();
$new_player = $db->insert('<ezrpg>players', $insert);

See Also

affected

public function affected()

Returns the number of affected rows from the last query executed.

Returns

An integer representing the number of affected rows, or -1 if the query failed.

connect

protected function connect()

Connects to a MySQL server and selects a database.

Parameters

$hostDatabase server
$usernameUsername to login with
$passwordPassword to login with
$dbnameName of database

Returns

True if there were no errors.

Throws a DbException if there was a connection problem.

public $query_count
Keeps track of number of queries made in this database connection.
public $error
Contains the last SQL error.
public $query
Contains the last query executed.
public $isConnected
A boolean showing the status of the database connection.
protected $db
Contains a MySQL link identifier.
public function __construct($host = 'localhost',
$username = 'root',
$password = '',
$dbname = '')
Saves the connection data for later use.
public function __destruct()
Closes the connection to the MySQL server.
public function execute($query,  
$params = )
Checks if a connection is made, otherwise first calls connect.
protected function connect()
Connects to a MySQL server and selects a database.
public function fetch(&$result)
Fetches the data from the result set and returns it as an object.
public function fetchArray(&$result)
Fetches the data from the result set and returns it as an array.
public function fetchAll(&$result,  
$return_array =  false)
Fetches all the data from a result set and returns it as an array of results.
public function fetchRow($query,  
$params = )
Combines execute and fetch to a single function for fetching single rows of data.
public function numRows(&$result)
Uses mysql_num_rows on a result set to return the number of rows.
public function insert($table,
$data)
Takes an array to generate an INSERT query, then executes it.
public function affected()
Returns the number of affected rows from the last query executed.
Factory class for database drivers.
Extends Exception class to specify a database error.
Close