From 3c023b12c3d27f0e2773e671b854e52e2dc0d1d6 Mon Sep 17 00:00:00 2001
From: admin To submit a query, use the following function: 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: 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. 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: 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. Returns the last query that was run (the query string, not the result). Example: The following two functions help simplify the process of writing database INSERTs and UPDATEs. This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example: The first parameter is the table name, the second is an associative array with the data to be inserted. The above example produces: This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example: 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: There are several ways to generate query results:
Queries
+$this->db->query();
+
$this->db->query('YOUR QUERY HERE');
-$query = $this->db->query('YOUR QUERY HERE');
+$this->db->simple_query();
+
+Escaping Queries
+
+
Escaping Queries$sql = "INSERT INTO table (title) VALUES('".$this->db->escape_str($title)."')";
-
-
@@ -114,51 +121,6 @@ $this->db->query($sql, array(3, 'live', 'Rick'));
Query Bindings
-
-
-
Query Helper Functions$this->db->last_query();
-
-$str = $this->db->last_query();
-
-
-
-
-// Produces: SELECT * FROM sometable....
-$this->db->insert_string();
-$data = array('name' => $name, 'email' => $email, 'url' => $url);
-
-
-
-$str = $this->db->insert_string('table_name', $data);
-INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@your-site.com', 'www.your-site.com')
-
-
-
-$this->db->update_string();
-$data = array('name' => $name, 'email' => $email, 'url' => $url);
-
-
-
-$where = "author_id = 1 AND status = 'active'";
-
-$str = $this->db->update_string('table_name', $data, $where);
- UPDATE exp_weblog SET name = 'Rick', email = 'rick@your-site.com', url = 'www.your-site.com' WHERE author_id = 1 AND status = 'active'
-
-
diff --git a/user_guide/libraries/database/results.html b/user_guide/libraries/database/results.html
index a9377764e..342e7e4b4 100644
--- a/user_guide/libraries/database/results.html
+++ b/user_guide/libraries/database/results.html
@@ -64,7 +64,7 @@ Query Results
-Query Results
+Generating Query Results
The following functions provide useful information when dealing with query results.
- -The number of rows returned by the query. Note: $query is the variable that the query was assigned to:
- -$query = $this->db->query('SELECT * FROM my_table');
-echo $query->num_rows();
-
-
-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();
-
-
-
-The insert ID number when performing database inserts.
- -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. - - -Outputs the database version you are running:
- -echo $this->db->version();
-
-
--
cgit v1.2.3-24-g4f1b