DB | Summit

DB is the main class for accessing a database. Although currently there is only a MySQL connection the configuration file will allow you to choose what type of connection to create. This will allow your application to support multiple database servers.


Configuration

Below are the configuration variables for the database.

Option Description
driver Database (mysql currently supported)
host Database server (generally "localhost")
username Username to connect to database
password Password to connect to database
database Name of the database to connect to
prefix A prefix to the tables you are accessing. Methods Delete, Insert, Update automatically add the prefix for you.


Methods


Init($config)

The Init method connects to the database server with the configuration you give.

Usage:
DB::Init($config);


Delete($table, $where)

The Delete method deletes records from your database by specifying a table and possible a where clause.

Usage:
DB::Delete('table', array('fieldname' => 'fieldvalue', 'fieldname2' => 'fieldvalue2');

Usage:
DB::Example('pets', array('petname' => 'Sam');


Escape($value)

Escape will escape values from allowing SQL injection attacks. This method is recommended to be used on all data used to access the database from the users input.

Usage:
DB::Escape($value)

Example:
$query = DB::Query("SELECT `petid` FROM `pets` WHERE `petname` = '".DB::Escape($_POST['petname'])."'");


FetchAssoc($query)

FetchAssoc will return an associative array of the results from your query.

Usage:
DB::FetchAssoc($query);

Example:
$query = DB::Query("SELECT `petname` FROM `pets`");
foreach(DB::FetchAssoc($query) as $results) {
echo $results['petname']."<br />";
}


FetchFirstAssoc($query)

FetchFirstAssoc does the same thing FetchAssoc does, however, it only returns the first row.

Usage:
DB::FetchAssoc($query);

Example:
$query = DB::Query("SELECT `petname` FROM `pets`");
$pet = DB::FetchFirstAssoc($query);
echo $pet['petname'];


FetchNthAssoc($query, $row)

FetchNthAssoc does the same thing as FetchFirstAssoc however it allows you to sepcify which row is returned. This method accounts for the 0 index so if you want the first row you must use 1 and not 0.

Usage:
DB::FetchAssoc($query);

Example:
$query = DB::Query("SELECT `petname` FROM `pets`");
$pet = DB::FetchNthAssoc($query, 3);
echo $pet['petname'];


Insert($table, $values)

Insert allows you to easily insert a row into a table by using key/value pairs from an array.

Usage:
DB::Insert('pets', array('name' => 'Jake', 'color' => 'Brown', 'type' => 'Dog', 'breed' => 'German Shepard'));


InsertId()

When you do an INSERT into a table that has an auto increment field, this method will get the id from the last INSERT query ran.

Usage:
$userid = DB::InsertId();


NumRows($query)

This method returns the number of rows from a SELECT query.

Usage:
$rows = DB::NumRows($query);

Example:
$query = DB::Query("SELECT `petname` FROM `pets`");
echo 'Rows: '.DB::NumRows($query);


Query($query_string)

Query is the method you use to run an SQL query. It returns the database resource so it can be used to get the data pulled from the database. If you have the "prefix" configuration variable set, you can use it by using the constant "TABLE_PREFIX".

Usage:
$query = DB::Query("SELECT * FROM `pets`"); $query = DB::Query("SELECT * FROM `".TABLE_PREFIX."pets`");


Update($table, $values, $where)

This methods makes it simple to update records in a database.

Usage:
DB::Update('tablename', array('fieldname1' => 'value1', 'fieldname2' => 'value2'), array('fieldname3' => 'value3'));

Example:
DB::Update('pets', array('name' => 'Jake', 'color' => 'Grey'), array('petid' => 6));








Featured Products