Saturday, July 16, 2011

codeIgniter database methods

The database class provides a wrapper of most common functions used for database access, and also others which simplify common queries. All queries performed with the database through the database abstraction layer can be analyzed for the execution time and further optimization.

Contents

[edit] Available Methods

[edit] Commonly Used

[edit] $db->query

Executes an SQL query with your database.

[edit] $db->simple_select

See Simple Select.

[edit] $db->fetch_array

Returns an array of values for the first row (can be iterated through with a while statement)
Example:

$query=$db->query("SELECT * FROM Table [WHERE Field='Value']");
while($result=$db->fetch_array($query))
{
   $data1=$result['FieldName1'];
   $data2=$result['FieldName2'];
  (etc.)
}

[edit] $db->fetch_field

See Fetch_Field.

[edit] $db->num_rows

Returns the number of rows in the query

[edit] $db->insert_id

Returns the insert id (The id of the primary key) of the insert query just run

[edit] $db->insert_query

Runs an insert query on a table in a database. Receive two parameters:
  1. string - The table name to perform the query on.
  2. array - An array of fields and their values.
Build an insert query from the array and executes the query using $db->query

[edit] $db->update_query

Runs an update query on a table in a database

[edit] $db->delete_query

Used to perform a delete query on a table in a database

[edit] $db->escape_string

Replaces addslashes, escapes data before being used in a query

[edit] Others

[edit] $db->connect

Connects a new database.

[edit] $db->select_db

Selects the database for the current MySQL Session

[edit] $db->explain_query

Helps to explain queries run from on database from the current session for debugging purposes.

[edit] $db->data_seek

Moves the internal pointer to the specified column

[edit] $db->close

Closes the connection to the currently open database

[edit] $db->errno

Returns the error number (if any) of the specified query resource

[edit] $db->error

Returns the error string (if any) of the specified query resource

[edit] $db->dberror

Outputs an error message (if any) of the specified query resource

[edit] $db->affected_rows

Returns the amount of affected rows from a "write" query

[edit] $db->num_fields

Returns the number of fields of the specified query resource

[edit] $db->list_tables

Returns the tables in the current open database

[edit] $db->table_exists

Returns true if the specified table exists

[edit] $db->field_exists

Returns true if the specified field exists

[edit] $db->shutdown_query

Runs a query thats performed when php is done parsing the file

[edit] $db->select_query

Runs a complex query (join queries). (Note: Depreciated in MyBB 1.4)

[edit] $db->get_version

Returns the version number of the database server being used

[edit] $db->optimize_table

Runs an optimize query on a table

[edit] $db->analyze_table

Runs an analyze query on a table

[edit] $db->show_create_table

Return the "create table" command for a specific table.

[edit] $db->show_fields_from

Show the "show fields from" command for a specific table.

[edit] $db->is_fulltext

Returns whether or not the table contains a fulltext index.

[edit] $db->supports_fulltext

Returns whether or not this database engine supports fulltext indexing.

[edit] $db->supports_fulltext_boolean

Returns whether or not this database engine supports boolean fulltext matching.

[edit] $db->create_fulltext_index

Creates a fulltext index on the specified column in the specified table with optional index name.

[edit] $db->drop_index

Drop an index with the specified name from the specified table

Bind Some Event to Element and trigger that function when click the element

Scenario: Suppose if we used same click event function in various web pages. if you want do some logic some page button for that need to re...