From 3c023b12c3d27f0e2773e671b854e52e2dc0d1d6 Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 24 Sep 2006 03:04:10 +0000 Subject: --- user_guide/libraries/database/queries.html | 62 ++++++------------------------ 1 file changed, 12 insertions(+), 50 deletions(-) (limited to 'user_guide/libraries/database/queries.html') diff --git a/user_guide/libraries/database/queries.html b/user_guide/libraries/database/queries.html index bfba04700..f5a06e3a9 100644 --- a/user_guide/libraries/database/queries.html +++ b/user_guide/libraries/database/queries.html @@ -66,17 +66,26 @@ Queries

Queries

+

$this->db->query();

+

To submit a query, use the following function:

$this->db->query('YOUR QUERY HERE'); -

The query() function returns a database result object -which you can use to show your results. You will typically assign the query to your own variable, like this:

+

The query() function returns a database result object when "read" type queries are run, +which you can use to show your results. When "write" type queries are run it simply returns TRUE or FALSE +depending on success or failure. When retrieving data you will typically assign the query to your own variable, like this:

$query = $this->db->query('YOUR QUERY HERE'); +

$this->db->simple_query();

+ +

This is a simplified version of the $this->db->query() function. It ONLY returns TRUE/FALSE on success or failure. +It DOES NOT return a database result set, nor does it set the query timer, or compile bind data, or store your query for debugging. +It simply lets you submit a query. Most users will rarely use this function.

-

Escaping Queries

+ +


Escaping Queries

It's a very good security practice to escape your data before sumbiting it into your database. Code Igniter has two functions that help you do this:

@@ -94,12 +103,10 @@ Most of the time you'll use the above function rather then this one. Use the fun $sql = "INSERT INTO table (title) VALUES('".$this->db->escape_str($title)."')"; - -


Query Bindings

@@ -114,51 +121,6 @@ $this->db->query($sql, array(3, 'live', 'Rick'));

The question marks in the query are automatically replaced with the values in the array in the second parameter of the query function.

The secondary benefit of using binds is that the values are automatically escaped, producing safer queries. You don't have to remember to manually escape data; the engine does it automatically for you.

- - -


Query Helper Functions

- - -

$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' - - -- cgit v1.2.3-24-g4f1b