From 6bc014118c087c1c29ec0c1e509598218e3b5bc0 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 24 Sep 2006 03:03:51 +0000 Subject: --- user_guide/libraries/database/helpers.html | 157 +++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 user_guide/libraries/database/helpers.html (limited to 'user_guide') diff --git a/user_guide/libraries/database/helpers.html b/user_guide/libraries/database/helpers.html new file mode 100644 index 000000000..a5151e35e --- /dev/null +++ b/user_guide/libraries/database/helpers.html @@ -0,0 +1,157 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.1

+
+ + + + + + + + + +
+ + + + +
+ + + +
+ + +

Query Helper Functions

+ + +

$query->num_rows()

+

The number of rows returned by the query. Note: In this example, $query is the variable that the query result object is assigned to:

+ +$query = $this->db->query('SELECT * FROM my_table');

+echo $query->num_rows(); +
+ +

$query->num_fields()

+

The number of FIELDS returned by the query. Make sure to call the function using your query result object:

+ +$query = $this->db->query('SELECT * FROM my_table');

+echo $query->num_fields(); +
+ + +

$this->db->insert_id()

+

The insert ID number when performing database inserts.

+ +

$this->db->affected_rows()

+

Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).

+

Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the +correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file.

+ + +

$this->db->version()

+

Outputs the database version you are running:

+ +echo $this->db->version(); + +

$this->db->last_query();

+ +

Returns the last query that was run (the query string, not the result). Example:

+ +$str = $this->db->last_query();
+
+// Produces: SELECT * FROM sometable.... +
+ + +

The following two functions help simplify the process of writing database INSERTs and UPDATEs.

+ + +

$this->db->insert_string();

+

This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example:

+ +$data = array('name' => $name, 'email' => $email, 'url' => $url);
+
+$str = $this->db->insert_string('table_name', $data); +
+ +

The first parameter is the table name, the second is an associative array with the data to be inserted. The above example produces:

+INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@your-site.com', 'www.your-site.com') + + + +

$this->db->update_string();

+

This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:

+ +$data = array('name' => $name, 'email' => $email, 'url' => $url);
+
+$where = "author_id = 1 AND status = 'active'"; +

+$str = $this->db->update_string('table_name', $data, $where); +
+ +

The first parameter is the table name, the second is an associative array with the data to be inserted, and the third parameter is the "where" clause. The above example produces:

+ UPDATE exp_weblog SET name = 'Rick', email = 'rick@your-site.com', url = 'www.your-site.com' WHERE author_id = 1 AND status = 'active' + + + + +
+ + + + + + + \ No newline at end of file -- cgit v1.2.3-24-g4f1b