You are not logged in (Sign In)

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 the insert() 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 $table that match the $where clause 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 $query is specified, data from the previous query will be used. $index_by_field will return an associate array rather than an indexed one, using the value of the field for the key. E.G. fetch(NULL, “id”) will return array([5] = array(id ⇒ 5, name ⇒ robert)); instead of array([0] = array(id ⇒ 5, name ⇒ robert));
  • fetchRow ($offset = 0, $query = NULL) - Similar to fetch() however allows you to specify a particular index. E.G. fetchRow(1), when used on a data set like array([0] = array(’id’ ⇒ 1, name ⇒ ‘fred), [1] ⇒ array(’id’ ⇒ 2, name ⇒ ‘bob’)) would return array(’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 set array([0] = array(’id’ ⇒ 1, name ⇒ ‘fred), [1] ⇒ array(’id’ ⇒ 2, name ⇒ ‘bob’)), giving the function call fetchCol(”id”) would return array(1, 2)
  • fetchVar ($varName, $offset = 0, $query = NULL) - Works just like fetchRow however 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.
 
You are not logged in
developer-resources/database-wrapper-api.txt · Last modified: 2007/05/27 23:46 by alistair