Database Wrapper API
Symphony uses a simple Database Wrapper for all database communication.
Accessing the global DB connection object
A connection to the DB is established automatically, and a single object reference passed around to various parts of Symphony. Depending on where you are, the code for accessing it is slightly different.
- Campfire Interface files you can use
$DB→{method} - Campfire service driver class is
$this→_parent→_db→{method}, however to make your life easier, set up an alias in the driver constructor like so$this→_db = $this→_parent→_db
The API
Below is a list of the most useful methods publicly available, I.E. methods that do not have a preceeding double underscore in its name.
setPrefix($prefix)- Allows you to change the current prefix value. This is useful if you need to switch between different table sets
isConnected()- returns true|false
getSelected()- returns the name of the database currently connected to
connect($host = NULL, $user = NULL, $password = NULL, $port = “3306”)- allows you to establish a connection.
setCharacterSet($set = “utf8”)- change the runtime character set
setCharacterEncoding($set = “utf8”)- change the runtime character encoding
select ($db = NULL)- allows you to select a different database
cleanFields(&$array)- given an array, this will clean all values in the array, making them suitable for use in an SQL query
insert($fields, $table, $method=”INSERT”)- provided with an associative array of fields, {field_name} ⇒ {value}, table name and optional method, this will insert data into your tables. E.G.$db→insert(array(’id’ ⇒ 1), ‘tbl_test’);
update($fields, $table, $where)- Similar to theinsert()function, this requires a where clause. E.G.$db→ update(array(’id’ ⇒ 3), ‘tbl_test’, ‘WHERE `id` = 1’);
delete($table, $where)- Will delete any rows from$tablethat match the$whereclause critera.
close ()- Closes the database connection
query ($query)- Run an arbitary SQL statement
numOfRows ()- Returns the number of rows retrived from the last query
getInsertID ()- Returns the ID of the last insert
fetch ($query = NULL, $index_by_field = NULL)- Returns sets of data. If no$queryis specified, data from the previous query will be used.$index_by_fieldwill return an associate array rather than an indexed one, using the value of the field for the key. E.G.fetch(NULL, “id”)will returnarray([5] = array(id ⇒ 5, name ⇒ robert));instead ofarray([0] = array(id ⇒ 5, name ⇒ robert));
fetchRow ($offset = 0, $query = NULL)- Similar tofetch()however allows you to specify a particular index. E.G.fetchRow(1), when used on a data set likearray([0] = array(’id’ ⇒ 1, name ⇒ ‘fred), [1] ⇒ array(’id’ ⇒ 2, name ⇒ ‘bob’))would returnarray(’id’ ⇒ 2, name ⇒ ‘bob’)
fetchXML($query = NULL)- Tries to convert the data set from a query into XML.
fetchCol ($name, $query = NULL)- Returns an array of values from a specific column in a dataset. E.G. the data setarray([0] = array(’id’ ⇒ 1, name ⇒ ‘fred), [1] ⇒ array(’id’ ⇒ 2, name ⇒ ‘bob’)), giving the function callfetchCol(”id”)would returnarray(1, 2)
fetchVar ($varName, $offset = 0, $query = NULL)- Works just likefetchRowhowever this time you can return a single value instead of an entire row.
flush()- Clears any previous results from the active object
debug()- returns an array of errors
import($sql)- splits a large, multi query SQL string into single queries and executes them singularly.