diff options
Diffstat (limited to 'user_guide/database')
-rw-r--r-- | user_guide/database/active_record.html | 824 | ||||
-rw-r--r-- | user_guide/database/caching.html | 220 | ||||
-rw-r--r-- | user_guide/database/call_function.html | 118 | ||||
-rw-r--r-- | user_guide/database/configuration.html | 164 | ||||
-rw-r--r-- | user_guide/database/connecting.html | 188 | ||||
-rw-r--r-- | user_guide/database/examples.html | 217 | ||||
-rw-r--r-- | user_guide/database/fields.html | 163 | ||||
-rw-r--r-- | user_guide/database/forge.html | 234 | ||||
-rw-r--r-- | user_guide/database/helpers.html | 151 | ||||
-rw-r--r-- | user_guide/database/index.html | 99 | ||||
-rw-r--r-- | user_guide/database/queries.html | 158 | ||||
-rw-r--r-- | user_guide/database/results.html | 259 | ||||
-rw-r--r-- | user_guide/database/table_data.html | 113 | ||||
-rw-r--r-- | user_guide/database/transactions.html | 200 | ||||
-rw-r--r-- | user_guide/database/utilities.html | 314 |
15 files changed, 0 insertions, 3422 deletions
diff --git a/user_guide/database/active_record.html b/user_guide/database/active_record.html deleted file mode 100644 index 17c58c9f1..000000000 --- a/user_guide/database/active_record.html +++ /dev/null @@ -1,824 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Active Record : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Active Record -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - -<br clear="all" /> - -<!-- START CONTENT --> -<div id="content"> - -<h1>Active Record Class</h1> - -<p>CodeIgniter uses a modified version of the Active Record Database Pattern. -This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. -In some cases only one or two lines of code are necessary to perform a database action. -CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface.</p> - -<p>Beyond simplicity, a major benefit to using the Active Record features is that it allows you to create database independent applications, since the query syntax -is generated by each database adapter. It also allows for safer queries, since the values are escaped automatically by the system.</p> - -<p class="important"><strong>Note:</strong> If you intend to write your own queries you can disable this class in your database config file, allowing the core database library and adapter to utilize fewer resources.<br /></p> - -<ul> -<li><a href="#select">Selecting Data</a></li> -<li><a href="#insert">Inserting Data</a></li> -<li><a href="#update">Updating Data</a></li> -<li><a href="#delete">Deleting Data</a></li> -<li><a href="#chaining">Method Chaining</a></li> -<li><a href="#caching">Active Record Caching</a></li> -</ul> - -<h1><a name="select"> </a>Selecting Data</h1> - -<p>The following functions allow you to build SQL <strong>SELECT</strong> statements.</p> - -<p><strong>Note: If you are using PHP 5 you can use method chaining for more compact syntax. This is described at the end of the page.</strong></p> - - -<h2>$this->db->get();</h2> - -<p>Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table:</p> - -<code>$query = $this->db->get('mytable');<br /> -<br /> -// Produces: SELECT * FROM mytable</code> - -<p>The second and third parameters enable you to set a limit and offset clause:</p> - -<code>$query = $this->db->get('mytable', 10, 20);<br /> -<br /> -// Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)</code> - -<p>You'll notice that the above function is assigned to a variable named <kbd>$query</kbd>, which can be used to show the results:</p> - -<code>$query = $this->db->get('mytable');<br /> -<br /> -foreach ($query->result() as $row)<br /> -{<br /> - echo $row->title;<br /> -}</code> - -<p>Please visit the <a href="results.html">result functions</a> page for a full discussion regarding result generation.</p> - - -<h2>$this->db->get_where();</h2> - -<p>Identical to the above function except that it permits you to add a "where" clause in the second parameter, -instead of using the db->where() function:</p> - -<code>$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);</code> - -<p>Please read the about the where function below for more information.</p> -<p class="important">Note: get_where() was formerly known as getwhere(), which has been removed</p> - -<h2>$this->db->select();</h2> -<p>Permits you to write the SELECT portion of your query:</p> -<p><code> -$this->db->select('title, content, date');<br /> -<br /> -$query = $this->db->get('mytable');<br /> -<br /> -// Produces: SELECT title, content, date FROM mytable</code></p> -<p class="important"><strong>Note:</strong> If you are selecting all (*) from a table you do not need to use this function. When omitted, CodeIgniter assumes you wish to SELECT *</p> - -<p>$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.</p> -<p><code>$this->db->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4') AS amount_paid', FALSE); <br /> -$query = $this->db->get('mytable');<br /> -</code></p> -<h2>$this->db->select_max();</h2> -<p>Writes a "SELECT MAX(field)" portion for your query. You can optionally include a second parameter to rename the resulting field.</p> -<p><code> -$this->db->select_max('age');<br /> -$query = $this->db->get('members');<br /> - -// Produces: SELECT MAX(age) as age FROM members<br /> -<br /> -$this->db->select_max('age', 'member_age');<br /> -$query = $this->db->get('members');<br /> -// Produces: SELECT MAX(age) as member_age FROM members</code></p> - -<h2>$this->db->select_min();</h2> -<p>Writes a "SELECT MIN(field)" portion for your query. As with <dfn>select_max()</dfn>, You can optionally include a second parameter to rename the resulting field.</p> -<p><code> -$this->db->select_min('age');<br /> -$query = $this->db->get('members');<br /> -// Produces: SELECT MIN(age) as age FROM members</code></p> - -<h2>$this->db->select_avg();</h2> -<p>Writes a "SELECT AVG(field)" portion for your query. As with <dfn>select_max()</dfn>, You can optionally include a second parameter to rename the resulting field.</p> -<p><code> -$this->db->select_avg('age');<br /> -$query = $this->db->get('members');<br /> -// Produces: SELECT AVG(age) as age FROM members</code></p> - -<h2>$this->db->select_sum();</h2> -<p>Writes a "SELECT SUM(field)" portion for your query. As with <dfn>select_max()</dfn>, You can optionally include a second parameter to rename the resulting field.</p> -<p><code> -$this->db->select_sum('age');<br /> -$query = $this->db->get('members');<br /> -// Produces: SELECT SUM(age) as age FROM members</code></p> - -<h2>$this->db->from();</h2> - -<p>Permits you to write the FROM portion of your query:</p> - -<code> -$this->db->select('title, content, date');<br /> -$this->db->from('mytable');<br /> -<br /> -$query = $this->db->get();<br /> -<br /> -// Produces: SELECT title, content, date FROM mytable</code> - -<p class="important">Note: As shown earlier, the FROM portion of your query can be specified in the <dfn>$this->db->get()</dfn> function, so use whichever method -you prefer.</p> - -<h2>$this->db->join();</h2> - -<p>Permits you to write the JOIN portion of your query:</p> - -<code> -$this->db->select('*');<br /> -$this->db->from('blogs');<br /> -$this->db->join('comments', 'comments.id = blogs.id');<br /> -<br /> -$query = $this->db->get();<br /> -<br /> -// Produces: <br /> -// SELECT * FROM blogs<br /> -// JOIN comments ON comments.id = blogs.id<br /> -</code> - -<p>Multiple function calls can be made if you need several joins in one query.</p> - -<p>If you need a specific type of JOIN you can specify it via the third parameter of the function. -Options are: left, right, outer, inner, left outer, and right outer.</p> - -<code> -$this->db->join('comments', 'comments.id = blogs.id', <strong>'left'</strong>);<br /> -<br /> -// Produces: LEFT JOIN comments ON comments.id = blogs.id</code> - - - - - -<h2>$this->db->where();</h2> -<p>This function enables you to set <strong>WHERE</strong> clauses using one of four methods:</p> - -<p class="important"><strong>Note:</strong> All values passed to this function are escaped automatically, producing safer queries.</p> - -<ol> - <li><strong>Simple key/value method:</strong> - - <code>$this->db->where('name', $name); - <br /><br />// Produces: WHERE name = 'Joe' </code> - - <p>Notice that the equal sign is added for you.</p> - - <p>If you use multiple function calls they will be chained together with <var>AND</var> between them:</p> - - <code>$this->db->where('name', $name);<br /> - $this->db->where('title', $title);<br /> - $this->db->where('status', $status); - <br /><br />// WHERE name = 'Joe' AND title = 'boss' AND status = 'active' </code> </li> - - <li><strong>Custom key/value method:</strong> - - <p>You can include an operator in the first parameter in order to control the comparison:</p> - - <code>$this->db->where('name !=', $name);<br /> - $this->db->where('id <', $id); - <br /><br />// Produces: WHERE name != 'Joe' AND id < 45 </code> </li> - <li><strong>Associative array method:</strong> - - - <code> - $array = array('name' => $name, 'title' => $title, 'status' => $status);<br /><br /> - - $this->db->where($array); - <br /><br />// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active' </code> - - <p>You can include your own operators using this method as well:</p> - - <code> - $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);<br /><br /> - - $this->db->where($array);</code> </li> - <li><strong>Custom string:</strong> - - <p>You can write your own clauses manually:</p> - - <code> - $where = "name='Joe' AND status='boss' OR status='active'";<br /><br /> - $this->db->where($where);</code></li> - </ol> - - -<p>$this->db->where() accepts an optional third parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks.</p> -<p><code> $this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);<br /> -</code></p> -<h2>$this->db->or_where();</h2> -<p>This function is identical to the one above, except that multiple instances are joined by OR:</p> - -<code> -$this->db->where('name !=', $name);<br /> -$this->db->or_where('id >', $id); -<br /> -<br />// Produces: WHERE name != 'Joe' OR id > 50</code> - -<p class="important">Note: or_where() was formerly known as orwhere(), which has been removed.</p> - - -<h2>$this->db->where_in();</h2> -<p>Generates a WHERE field IN ('item', 'item') SQL query joined with AND if appropriate</p> -<p><code> - $names = array('Frank', 'Todd', 'James');<br /> - $this->db->where_in('username', $names);<br /> - // Produces: WHERE username IN ('Frank', 'Todd', 'James')</code></p> - -<h2>$this->db->or_where_in();</h2> -<p>Generates a WHERE field IN ('item', 'item') SQL query joined with OR if appropriate</p> -<p><code> - $names = array('Frank', 'Todd', 'James');<br /> - $this->db->or_where_in('username', $names);<br /> - // Produces: OR username IN ('Frank', 'Todd', 'James')</code></p> - -<h2>$this->db->where_not_in();</h2> -<p>Generates a WHERE field NOT IN ('item', 'item') SQL query joined with AND if appropriate</p> -<p><code> - $names = array('Frank', 'Todd', 'James');<br /> - $this->db->where_not_in('username', $names);<br /> - // Produces: WHERE username NOT IN ('Frank', 'Todd', 'James')</code></p> - -<h2>$this->db->or_where_not_in();</h2> -<p>Generates a WHERE field NOT IN ('item', 'item') SQL query joined with OR if appropriate</p> -<p><code> - $names = array('Frank', 'Todd', 'James');<br /> - $this->db->or_where_not_in('username', $names);<br /> - // Produces: OR username NOT IN ('Frank', 'Todd', 'James')</code></p> - -<h2>$this->db->like();</h2> -<p>This function enables you to generate <strong>LIKE</strong> clauses, useful for doing searches.</p> - -<p class="important"><strong>Note:</strong> All values passed to this function are escaped automatically.</p> - - -<ol> - <li><strong>Simple key/value method:</strong> - - <code>$this->db->like('title', 'match'); - <br /><br />// Produces: WHERE title LIKE '%match%' </code> - - <p>If you use multiple function calls they will be chained together with <var>AND</var> between them:</p> - - <code>$this->db->like('title', 'match');<br /> - $this->db->like('body', 'match'); - <br /><br /> - // WHERE title LIKE '%match%' AND body LIKE '%match%</code> - If you want to control where the wildcard (%) is placed, you can use an optional third argument. Your options are 'before', 'after' and 'both' (which is the default). - <code>$this->db->like('title', 'match', 'before'); - <br /> - // Produces: WHERE title LIKE '%match' <br /> - <br /> - $this->db->like('title', 'match', 'after'); <br /> -// Produces: WHERE title LIKE 'match%' <br /> -<br /> - $this->db->like('title', 'match', 'both'); <br /> -// Produces: WHERE title LIKE '%match%' </code> </li> - -If you do not want to use the wildcard (%) you can pass to the optional third argument the option 'none'. - -<code> - $this->db->like('title', 'match', 'none'); <br /> -// Produces: WHERE title LIKE 'match' -</code> - - <li><strong>Associative array method:</strong> - - <code> - $array = array('title' => $match, 'page1' => $match, 'page2' => $match);<br /><br /> - - $this->db->like($array); - <br /><br />// WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%'</code></li> - </ol> - - -<h2>$this->db->or_like();</h2> -<p>This function is identical to the one above, except that multiple instances are joined by OR:</p> - -<code> -$this->db->like('title', 'match');<br /> -$this->db->or_like('body', $match); -<br /> -<br />// WHERE title LIKE '%match%' OR body LIKE '%match%'</code> - - - - -<p class="important">Note: or_like() was formerly known as orlike(), which has been removed.</p> -<h2>$this->db->not_like();</h2> -<p>This function is identical to <strong>like()</strong>, except that it generates NOT LIKE statements:</p> -<code> $this->db->not_like('title', 'match');<br /> -<br /> -// WHERE title NOT LIKE '%match%</code> -<h2>$this->db->or_not_like();</h2> -<p>This function is identical to <strong>not_like()</strong>, except that multiple instances are joined by OR:</p> -<code> $this->db->like('title', 'match');<br /> -$this->db->or_not_like('body', 'match'); <br /> -<br /> -// WHERE title LIKE '%match% OR body NOT LIKE '%match%'</code> -<h2>$this->db->group_by();</h2> -<p>Permits you to write the GROUP BY portion of your query:</p> - -<code>$this->db->group_by("title"); -<br /><br />// Produces: GROUP BY title -</code> - -<p>You can also pass an array of multiple values as well:</p> - -<code>$this->db->group_by(array("title", "date")); -<br /> -<br />// Produces: GROUP BY title, date</code> - -<p class="important">Note: group_by() was formerly known as groupby(), which has been removed. </p> - -<h2> $this->db->distinct();<br /> -</h2> -<p>Adds the "DISTINCT" keyword to a query</p> -<p><code>$this->db->distinct();<br /> - $this->db->get('table');<br /> - <br /> - // Produces: SELECT DISTINCT * FROM table</code></p> -<h2>$this->db->having();</h2> -<p>Permits you to write the HAVING portion of your query. There are 2 possible syntaxes, 1 argument or 2:</p> - -<code>$this->db->having('user_id = 45'); -<br /> -// Produces: HAVING user_id = 45<br /> -<br /> -$this->db->having('user_id', 45); <br /> -// Produces: HAVING user_id = 45<br /> -<br /> -</code> - -<p>You can also pass an array of multiple values as well:</p> - - -<p><code>$this->db->having(array('title =' => 'My Title', 'id <' => $id)); <br /> - <br /> - // Produces: HAVING title = 'My Title', id < 45</code></p> -<p>If you are using a database that CodeIgniter escapes queries for, you can prevent escaping content by passing an optional third argument, and setting it to FALSE.</p> -<p><code>$this->db->having('user_id', 45); <br /> -// Produces: HAVING `user_id` = 45 in some databases such as MySQL - <br /> - $this->db->having('user_id', 45, FALSE); <br /> -// Produces: HAVING user_id = 45</code></p> -<h2>$this->db->or_having();</h2> -<p>Identical to having(), only separates multiple clauses with "OR".</p> -<h2>$this->db->order_by();</h2> -<p>Lets you set an ORDER BY clause. The first parameter contains the name of the column you would like to order by. -The second parameter lets you set the direction of the result. Options are <kbd>asc</kbd> or <kbd>desc</kbd>, or <kbd>random</kbd>. </p> - -<code>$this->db->order_by("title", "desc"); -<br /> -<br />// Produces: ORDER BY title DESC -</code> - -<p>You can also pass your own string in the first parameter:</p> - -<code>$this->db->order_by('title desc, name asc'); -<br /> -<br />// Produces: ORDER BY title DESC, name ASC -</code> - -<p>Or multiple function calls can be made if you need multiple fields.</p> - -<p><code>$this->db->order_by("title", "desc");<br /> - $this->db->order_by("name", "asc"); <br /> - <br /> - // Produces: ORDER BY title DESC, name ASC - </code></p> -<p class="important">Note: order_by() was formerly known as orderby(), which has been removed.</p> -<p class="important">Note: random ordering is not currently supported in Oracle or MSSQL drivers. These will default to 'ASC'.</p> -<h2>$this->db->limit();</h2> -<p>Lets you limit the number of rows you would like returned by the query:</p> - -<code> -$this->db->limit(10);<br /> -<br /> -// Produces: LIMIT 10</code> - - -<p>The second parameter lets you set a result offset.</p> - -<code> -$this->db->limit(10, 20);<br /> -<br /> -// Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)</code> - - -<h2>$this->db->count_all_results();</h2> - -<p>Permits you to determine the number of rows in a particular Active Record query. Queries will accept Active Record restrictors such as where(), or_where(), like(), or_like(), etc. Example:</p> -<code>echo $this->db->count_all_results('<var>my_table</var>');<br /> - -// Produces an integer, like 25<br /> -<br /> -$this->db->like('title', 'match');<br /> -$this->db->from('<var>my_table</var>');<br /> -echo $this->db->count_all_results();<br /> -// Produces an integer, like 17 </code> - -<h2>$this->db->count_all();</h2> - -<p>Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example:</p> - -<code>echo $this->db->count_all('<var>my_table</var>');<br /> -<br /> -// Produces an integer, like 25</code> - - - -<a name="insert"> </a> -<h1>Inserting Data</h1> - -<h2>$this->db->insert();</h2> -<p>Generates an insert string based on the data you supply, and runs the query. You can either pass an -<strong>array</strong> or an <strong>object</strong> to the function. Here is an example using an array:</p> - -<code> -$data = array(<br /> - 'title' => 'My title' ,<br /> - 'name' => 'My Name' ,<br /> - 'date' => 'My date'<br /> -);<br /> -<br /> -$this->db->insert('mytable', $data); -<br /><br /> -// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')</code> - -<p>The first parameter will contain the table name, the second is an associative array of values.</p> - -<p>Here is an example using an object:</p> - -<code> -/*<br /> - class Myclass {<br /> - var $title = 'My Title';<br /> - var $content = 'My Content';<br /> - var $date = 'My Date';<br /> - }<br /> -*/<br /> -<br /> -$object = new Myclass;<br /> -<br /> -$this->db->insert('mytable', $object); -<br /><br /> -// Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date')</code> - -<p>The first parameter will contain the table name, the second is an object.</p> - -<p class="important"><strong>Note:</strong> All values are escaped automatically producing safer queries.</p> - -<h2>$this->db->insert_batch();</h2> -<p>Generates an insert string based on the data you supply, and runs the query. You can either pass an -<strong>array</strong> or an <strong>object</strong> to the function. Here is an example using an array:</p> - -<code> -$data = array(<br/> - array(<br /> - 'title' => 'My title' ,<br /> - 'name' => 'My Name' ,<br /> - 'date' => 'My date'<br /> - ),<br /> - array(<br /> - 'title' => 'Another title' ,<br /> - 'name' => 'Another Name' ,<br /> - 'date' => 'Another date'<br /> - )<br/> -);<br /> -<br /> -$this->db->insert_batch('mytable', $data); -<br /><br /> -// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')</code> - -<p>The first parameter will contain the table name, the second is an associative array of values.</p> - -<p class="important"><strong>Note:</strong> All values are escaped automatically producing safer queries.</p> - - - -<h2>$this->db->set();</h2> -<p>This function enables you to set values for <dfn>inserts</dfn> or <dfn>updates</dfn>.</p> - -<p><strong>It can be used instead of passing a data array directly to the insert or update functions:</strong> </p> - -<code>$this->db->set('name', $name); -<br /> -$this->db->insert('mytable'); -<br /><br /> -// Produces: INSERT INTO mytable (name) VALUES ('{$name}')</code> - -<p>If you use multiple function called they will be assembled properly based on whether you are doing an insert or an update:</p> - -<code>$this->db->set('name', $name);<br /> -$this->db->set('title', $title);<br /> -$this->db->set('status', $status);<br /> -$this->db->insert('mytable'); </code> -<p><strong>set()</strong> will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE. To illustrate the difference, here is set() used both with and without the escape parameter.</p> -<p><code>$this->db->set('field', 'field+1', FALSE);<br /> - $this->db->insert('mytable'); <br /> - // gives INSERT INTO mytable (field) VALUES (field+1)<br /> - <br /> - $this->db->set('field', 'field+1');<br /> - $this->db->insert('mytable'); <br /> - // gives INSERT INTO mytable (field) VALUES ('field+1')</code></p> -<p>You can also pass an associative array to this function:</p> -<code> -$array = array('name' => $name, 'title' => $title, 'status' => $status);<br /><br /> - -$this->db->set($array);<br /> -$this->db->insert('mytable'); -</code> - -<p>Or an object:</p> - - -<code> -/*<br /> - class Myclass {<br /> - var $title = 'My Title';<br /> - var $content = 'My Content';<br /> - var $date = 'My Date';<br /> - }<br /> -*/<br /> -<br /> -$object = new Myclass;<br /> -<br /> -$this->db->set($object);<br /> -$this->db->insert('mytable'); -</code> - - - -<a name="update"> </a> -<h1>Updating Data</h1> - -<h2>$this->db->update();</h2> -<p>Generates an update string and runs the query based on the data you supply. You can pass an -<strong>array</strong> or an <strong>object</strong> to the function. Here is an example using -an array:</p> - -<code> -$data = array(<br /> - 'title' => $title,<br /> - 'name' => $name,<br /> - 'date' => $date<br /> - );<br /> -<br /> -$this->db->where('id', $id);<br /> -$this->db->update('mytable', $data); -<br /><br /> -// Produces:<br /> -// UPDATE mytable <br /> -// SET title = '{$title}', name = '{$name}', date = '{$date}'<br /> -// WHERE id = $id</code> - -<p>Or you can supply an object:</p> - -<code> -/*<br /> - class Myclass {<br /> - var $title = 'My Title';<br /> - var $content = 'My Content';<br /> - var $date = 'My Date';<br /> - }<br /> -*/<br /> -<br /> -$object = new Myclass;<br /> -<br /> -$this->db->where('id', $id);<br /> -$this->db->update('mytable', $object); -<br /> -<br /> -// Produces:<br /> -// UPDATE mytable <br /> -// SET title = '{$title}', name = '{$name}', date = '{$date}'<br /> -// WHERE id = $id</code> - - - -<p class="important"><strong>Note:</strong> All values are escaped automatically producing safer queries.</p> - -<p>You'll notice the use of the <dfn>$this->db->where()</dfn> function, enabling you to set the WHERE clause. -You can optionally pass this information directly into the update function as a string:</p> - -<code>$this->db->update('mytable', $data, "id = 4");</code> - -<p>Or as an array:</p> - -<code>$this->db->update('mytable', $data, array('id' => $id));</code> - -<p>You may also use the <dfn>$this->db->set()</dfn> function described above when performing updates.</p> - -<h2>$this->db->update_batch();</h2> -<p>Generates an update string based on the data you supply, and runs the query. You can either pass an -<strong>array</strong> or an <strong>object</strong> to the function. Here is an example using an array:</p> - -<code> -$data = array(<br/> - array(<br /> - 'title' => 'My title' ,<br /> - 'name' => 'My Name 2' ,<br /> - 'date' => 'My date 2'<br /> - ),<br /> - array(<br /> - 'title' => 'Another title' ,<br /> - 'name' => 'Another Name 2' ,<br /> - 'date' => 'Another date 2'<br /> - )<br/> -);<br /> -<br /> -$this->db->update_batch('mytable', $data, 'title'); -<br /><br /> -// Produces: <br /> -// UPDATE `mytable` SET `name` = CASE<br /> -// WHEN `title` = 'My title' THEN 'My Name 2'<br /> -// WHEN `title` = 'Another title' THEN 'Another Name 2'<br /> -// ELSE `name` END,<br /> -// `date` = CASE <br /> -// WHEN `title` = 'My title' THEN 'My date 2'<br /> -// WHEN `title` = 'Another title' THEN 'Another date 2'<br /> -// ELSE `date` END<br /> -// WHERE `title` IN ('My title','Another title')</code> - -<p>The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.</p> - -<p class="important"><strong>Note:</strong> All values are escaped automatically producing safer queries.</p> - - -<a name="delete"> </a> -<h1>Deleting Data</h1> - - - -<h2>$this->db->delete();</h2> -<p>Generates a delete SQL string and runs the query.</p> - -<code> -$this->db->delete('mytable', array('id' => $id)); -<br /><br /> -// Produces:<br /> -// DELETE FROM mytable <br /> -// WHERE id = $id</code> - -<p>The first parameter is the table name, the second is the where clause. You can also use the <dfn>where()</dfn> or <dfn>or_where()</dfn> functions instead of passing -the data to the second parameter of the function:</p> - -<p><code> $this->db->where('id', $id);<br /> - $this->db->delete('mytable'); <br /> - <br /> - // Produces:<br /> - // DELETE FROM mytable <br /> - // WHERE id = $id</code></p> -<p>An array of table names can be passed into delete() if you would like to delete data from more than 1 table.</p> -<p><code>$tables = array('table1', 'table2', 'table3');<br /> -$this->db->where('id', '5');<br /> -$this->db->delete($tables);</code></p> -<p>If you want to delete all data from a table, you can use the <dfn>truncate()</dfn> function, or <dfn>empty_table()</dfn>.</p> -<h2>$this->db->empty_table();</h2> -<p>Generates a delete SQL string and runs the query.<code> $this->db->empty_table('mytable'); <br /> - <br /> -// Produces<br /> -// DELETE FROM mytable</code></p> -<h2>$this->db->truncate();</h2> -<p>Generates a truncate SQL string and runs the query.</p> -<code> $this->db->from('mytable'); <br /> -$this->db->truncate(); <br /> -// or <br /> -$this->db->truncate('mytable'); <br /> -<br /> -// Produce:<br /> -// TRUNCATE mytable <br /> -</code> -<p class="important"><strong>Note:</strong> If the TRUNCATE command isn't available, truncate() will execute as "DELETE FROM table".</p> - -<h1><a name="chaining"> </a>Method Chaining</h1> - -<p>Method chaining allows you to simplify your syntax by connecting multiple functions. Consider this example:</p> - -<code> -<dfn>$this->db</dfn><kbd>-></kbd><var>select</var>('title')<kbd>-></kbd><var>from</var>('mytable')<kbd>-></kbd><var>where</var>('id', $id)<kbd>-></kbd><var>limit</var>(10, 20);<br /> -<br /> -$query = $this->db->get();</code> - -<p class="important"><strong>Note:</strong> Method chaining only works with PHP 5.</p> - -<p> </p> - -<h1><a name="caching"> </a>Active Record Caching</h1> - -<p>While not "true" caching, Active Record enables you to save (or "cache") certain parts of your queries for reuse at a later point in your script's execution. Normally, when an Active Record call is completed, all stored information is reset for the next call. With caching, you can prevent this reset, and reuse information easily.</p> - -<p>Cached calls are cumulative. If you make 2 cached select() calls, and then 2 uncached select() calls, this will result in 4 select() calls. There are three Caching functions available:</p> - -<h2>$this->db->start_cache()</h2> - -<p>This function must be called to begin caching. All Active Record queries of the correct type (see below for supported queries) are stored for later use.</p> - -<h2>$this->db->stop_cache()</h2> - -<p>This function can be called to stop caching.</p> - -<h2>$this->db->flush_cache()</h2> - -<p>This function deletes all items from the Active Record cache.</p> - -<p>Here's a usage example:</p> - -<p><code>$this->db->start_cache();<br /> -$this->db->select('field1');<br /> -$this->db->stop_cache();<br /><br /> -$this->db->get('tablename');<br /> -<br /> -//Generates: SELECT `field1` FROM (`tablename`)<br /> -<br /> -$this->db->select('field2');<br /> -$this->db->get('tablename');<br /> -<br /> -//Generates: SELECT `field1`, `field2` FROM (`tablename`)<br /> -<br /> -$this->db->flush_cache();<br /> -<br /> -$this->db->select('field2');<br /> -$this->db->get('tablename');<br /> -<br /> -//Generates: SELECT `field2` FROM (`tablename`)</code></p> - -<p class="important"> <strong>Note:</strong> The following statements can be cached: select, from, join, where, like, group_by, having, order_by, set</p> -<p> </p> -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="helpers.html">Query Helper Functions</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="transactions.html">Transactions</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html> diff --git a/user_guide/database/caching.html b/user_guide/database/caching.html deleted file mode 100644 index e6e72f269..000000000 --- a/user_guide/database/caching.html +++ /dev/null @@ -1,220 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Database Caching Class : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Database Caching Class -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - -<h1>Database Caching Class</h1> - -<p>The Database Caching Class permits you to cache your queries as text files for reduced database load.</p> - -<p class="important"><strong>Important:</strong> This class is initialized automatically by the database driver -when caching is enabled. Do NOT load this class manually.<br /><br /> - -<strong>Also note:</strong> Not all query result functions are available when you use caching. Please read this page carefully.</p> - -<h2>Enabling Caching</h2> - -<p>Caching is enabled in three steps:</p> - -<ul> -<li>Create a writable directory on your server where the cache files can be stored.</li> -<li>Set the path to your cache folder in your <dfn>application/config/database.php</dfn> file.</li> -<li>Enable the caching feature, either globally by setting the preference in your <dfn>application/config/database.php</dfn> file, or manually as described below.</li> -</ul> - -<p>Once enabled, caching will happen automatically whenever a page is loaded that contains database queries.</p> - - -<h2>How Does Caching Work?</h2> - -<p>CodeIgniter's query caching system happens dynamically when your pages are viewed. -When caching is enabled, the first time a web page is loaded, the query result object will -be serialized and stored in a text file on your server. The next time the page is loaded the cache file will be used instead of -accessing your database. Your database usage can effectively be reduced to zero for any pages that have been cached.</p> - -<p>Only <dfn>read-type</dfn> (SELECT) queries can be cached, since these are the only type of queries that produce a result. -<dfn>Write-type</dfn> (INSERT, UPDATE, etc.) queries, since they don't generate a result, will not be cached by the system.</p> - -<p>Cache files DO NOT expire. Any queries that have been cached will remain cached until you delete them. The caching system -permits you clear caches associated with individual pages, or you can delete the entire collection of cache files. -Typically you'll want to use the housekeeping functions described below to delete cache files after certain -events take place, like when you've added new information to your database.</p> - -<h2>Will Caching Improve Your Site's Performance?</h2> - -<p>Getting a performance gain as a result of caching depends on many factors. -If you have a highly optimized database under very little load, you probably won't see a performance boost. -If your database is under heavy use you probably will see an improved response, assuming your file-system is not -overly taxed. Remember that caching simply changes how your information is retrieved, shifting it from being a database -operation to a file-system one.</p> - -<p>In some clustered server environments, for example, caching may be detrimental since file-system operations are so intense. -On single servers in shared environments, caching will probably be beneficial. Unfortunately there is no -single answer to the question of whether you should cache your database. It really depends on your situation.</p> - -<h2>How are Cache Files Stored?</h2> - -<p>CodeIgniter places the result of EACH query into its own cache file. Sets of cache files are further organized into -sub-folders corresponding to your controller functions. To be precise, the sub-folders are named identically to the -first two segments of your URI (the controller class name and function name).</p> - -<p>For example, let's say you have a controller called <dfn>blog</dfn> with a function called <dfn>comments</dfn> that -contains three queries. The caching system will create a cache folder -called <kbd>blog+comments</kbd>, into which it will write three cache files.</p> - -<p>If you use dynamic queries that change based on information in your URI (when using pagination, for example), each instance of -the query will produce its own cache file. It's possible, therefore, to end up with many times more cache files than you have -queries.</p> - - -<h2>Managing your Cache Files</h2> - -<p>Since cache files do not expire, you'll need to build deletion routines into your application. For example, let's say you have a blog -that allows user commenting. Whenever a new comment is submitted you'll want to delete the cache files associated with the -controller function that serves up your comments. You'll find two delete functions described below that help you -clear data.</p> - - -<h2>Not All Database Functions Work with Caching</h2> - -<p>Lastly, we need to point out that the result object that is cached is a simplified version of the full result object. For that reason, -some of the query result functions are not available for use.</p> - -<p>The following functions <kbd>ARE NOT</kbd> available when using a cached result object:</p> - -<ul> -<li>num_fields()</li> -<li>field_names()</li> -<li>field_data()</li> -<li>free_result()</li> -</ul> - -<p>Also, the two database resources (result_id and conn_id) are not available when caching, since result resources only -pertain to run-time operations.</p> - - -<br /> - -<h1>Function Reference</h1> - - - -<h2>$this->db->cache_on() / $this->db->cache_off()</h2> - -<p>Manually enables/disables caching. This can be useful if you want to -keep certain queries from being cached. Example:</p> - -<code> -// Turn caching on<br /> -$this->db->cache_on();<br /> -$query = $this->db->query("SELECT * FROM mytable");<br /> -<br /> -// Turn caching off for this one query<br /> -$this->db->cache_off();<br /> -$query = $this->db->query("SELECT * FROM members WHERE member_id = '$current_user'");<br /> -<br /> -// Turn caching back on<br /> -$this->db->cache_on();<br /> -$query = $this->db->query("SELECT * FROM another_table"); -</code> - - -<h2>$this->db->cache_delete()</h2> - -<p>Deletes the cache files associated with a particular page. This is useful if you need to clear caching after you update your database.</p> - -<p>The caching system saves your cache files to folders that correspond to the URI of the page you are viewing. For example, if you are viewing -a page at <dfn>example.com/index.php/blog/comments</dfn>, the caching system will put all cache files associated with it in a folder -called <dfn>blog+comments</dfn>. To delete those particular cache files you will use:</p> - -<code>$this->db->cache_delete('blog', 'comments');</code> - -<p>If you do not use any parameters the current URI will be used when determining what should be cleared.</p> - - -<h2>$this->db->cache_delete_all()</h2> - -<p>Clears all existing cache files. Example:</p> - -<code>$this->db->cache_delete_all();</code> - - - - - - - - - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="call_function.html">Custom Function Calls</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="forge.html">Database manipulation with Database Forge</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/call_function.html b/user_guide/database/call_function.html deleted file mode 100644 index 4fc894743..000000000 --- a/user_guide/database/call_function.html +++ /dev/null @@ -1,118 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Custom Function Calls : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Custom Function Calls -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - -<h1>Custom Function Calls</h1> - -<h2>$this->db->call_function();</h2> - -<p>This function enables you to call PHP database functions that are not natively included in CodeIgniter, in a platform independent manner. -For example, lets say you want to call the <dfn>mysql_get_client_info()</dfn> function, which is <strong>not</strong> natively supported -by CodeIgniter. You could do so like this: -</p> - -<code>$this->db->call_function('<var>get_client_info</var>');</code> - -<p>You must supply the name of the function, <strong>without</strong> the <var>mysql_</var> prefix, in the first parameter. The prefix is added -automatically based on which database driver is currently being used. This permits you to run the same function on different database platforms. -Obviously not all function calls are identical between platforms, so there are limits to how useful this function can be in terms of portability.</p> - -<p>Any parameters needed by the function you are calling will be added to the second parameter.</p> - -<code>$this->db->call_function('<var>some_function</var>', $param1, $param2, etc..);</code> - - -<p>Often, you will either need to supply a database connection ID or a database result ID. The connection ID can be accessed using:</p> - -<code>$this->db->conn_id;</code> - -<p>The result ID can be accessed from within your result object, like this:</p> - -<code>$query = $this->db->query("SOME QUERY");<br /> -<br /> -<var>$query->result_id;</var></code> - - - - - - - - - - - - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="fields.html">Field MetaData</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="caching.html">Query Caching</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/configuration.html b/user_guide/database/configuration.html deleted file mode 100644 index 17a291ac2..000000000 --- a/user_guide/database/configuration.html +++ /dev/null @@ -1,164 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Database Configuration : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Configuration -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - - -<h1>Database Configuration</h1> - -<p>CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). -The config file is located at <samp>application/config/database.php</samp>. You can also set database connection values for specific <a href="../libraries/config.html">environments</a> by placing <strong>database.php</strong> it the respective environment config folder.</p> - -<p>The config settings are stored in a multi-dimensional array with this prototype:</p> - -<code>$db['default']['hostname'] = "localhost";<br /> -$db['default']['username'] = "root";<br /> -$db['default']['password'] = "";<br /> -$db['default']['database'] = "database_name";<br /> -$db['default']['dbdriver'] = "mysql";<br /> -$db['default']['dbprefix'] = "";<br /> -$db['default']['pconnect'] = TRUE;<br /> -$db['default']['db_debug'] = FALSE;<br /> -$db['default']['cache_on'] = FALSE;<br /> -$db['default']['cachedir'] = "";<br /> -$db['default']['char_set'] = "utf8";<br /> -$db['default']['dbcollat'] = "utf8_general_ci";<br /> -$db['default']['swap_pre'] = "";<br /> -$db['default']['autoinit'] = TRUE;<br /> -$db['default']['stricton'] = FALSE;</code> - -<p>The reason we use a multi-dimensional array rather than a more simple one is to permit you to optionally store -multiple sets of connection values. If, for example, you run multiple environments (development, production, test, etc.) -under a single installation, you can set up a connection group for each, then switch between groups as needed. -For example, to set up a "test" environment you would do this:</p> - -<code>$db['test']['hostname'] = "localhost";<br /> -$db['test']['username'] = "root";<br /> -$db['test']['password'] = "";<br /> -$db['test']['database'] = "database_name";<br /> -$db['test']['dbdriver'] = "mysql";<br /> -$db['test']['dbprefix'] = "";<br /> -$db['test']['pconnect'] = TRUE;<br /> -$db['test']['db_debug'] = FALSE;<br /> -$db['test']['cache_on'] = FALSE;<br /> -$db['test']['cachedir'] = "";<br /> -$db['test']['char_set'] = "utf8";<br /> -$db['test']['dbcollat'] = "utf8_general_ci";<br /> -$db['test']['swap_pre'] = "";<br /> -$db['test']['autoinit'] = TRUE;<br /> -$db['test']['stricton'] = FALSE;</code> - - -<p>Then, to globally tell the system to use that group you would set this variable located in the config file:</p> - -<code>$active_group = "test";</code> - -<p>Note: The name "test" is arbitrary. It can be anything you want. By default we've used the word "default" -for the primary connection, but it too can be renamed to something more relevant to your project.</p> - -<h3>Active Record</h3> - -<p>The <a href="active_record.html">Active Record Class</a> is globally enabled or disabled by setting the $active_record variable in the database configuration file to TRUE/FALSE (boolean). If you are not using the active record class, setting it to FALSE will utilize fewer resources when the database classes are initialized.</p> - -<code>$active_record = TRUE;</code> - -<p class="important"><strong>Note:</strong> that some CodeIgniter classes such as Sessions require Active Records be enabled to access certain functionality.</p> - -<h3>Explanation of Values:</h3> - -<ul> -<li><strong>hostname</strong> - The hostname of your database server. Often this is "localhost".</li> -<li><strong>username</strong> - The username used to connect to the database.</li> -<li><strong>password</strong> - The password used to connect to the database.</li> -<li><strong>database</strong> - The name of the database you want to connect to.</li> -<li><strong>dbdriver</strong> - The database type. ie: mysql, postgres, odbc, etc. Must be specified in lower case.</li> -<li><strong>dbprefix</strong> - An optional table prefix which will added to the table name when running <a href="active_record.html">Active Record</a> queries. This permits multiple CodeIgniter installations to share one database.</li> -<li><strong>pconnect</strong> - TRUE/FALSE (boolean) - Whether to use a persistent connection.</li> -<li><strong>db_debug</strong> - TRUE/FALSE (boolean) - Whether database errors should be displayed.</li> -<li><strong>cache_on</strong> - TRUE/FALSE (boolean) - Whether database query caching is enabled, see also <a href="caching.html">Database Caching Class</a>.</li> -<li><strong>cachedir</strong> - The absolute server path to your database query cache directory.</li> -<li><strong>char_set</strong> - The character set used in communicating with the database.</li> -<li><strong>dbcollat</strong> - The character collation used in communicating with the database. <p class="important"><strong>Note:</strong> For MySQL and MySQLi databases, this setting is only used as a backup if your server is running PHP < 5.2.3 or MySQL < 5.0.7 (and in table creation queries made with DB Forge). There is an incompatibility in PHP with mysql_real_escape_string() which can make your site vulnerable to SQL injection if you are using a multi-byte character set and are running versions lower than these. Sites using Latin-1 or UTF-8 database character set and collation are unaffected.</p></li> -<li><strong>swap_pre</strong> - A default table prefix that should be swapped with <var>dbprefix</var>. This is useful for distributed applications where you might run manually written queries, and need the prefix to still be customizable by the end user.</li> -<li><strong>autoinit</strong> - Whether or not to automatically connect to the database when the library loads. If set to false, the connection will take place prior to executing the first query.</li> -<li><strong>stricton</strong> - TRUE/FALSE (boolean) - Whether to force "Strict Mode" connections, good for ensuring strict SQL while developing an application.</li> -<li><strong>port</strong> - The database port number. To use this value you have to add a line to the database config array.<code>$db['default']['port'] = 5432;</code> -</ul> - -<p class="important"><strong>Note:</strong> Depending on what database platform you are using (MySQL, Postgres, etc.) -not all values will be needed. For example, when using SQLite you will not need to supply a username or password, and -the database name will be the path to your database file. The information above assumes you are using MySQL.</p> - - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="examples.html">Quick Start: Usage Examples</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="connecting.html">Connecting to your Database</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/connecting.html b/user_guide/database/connecting.html deleted file mode 100644 index f86602269..000000000 --- a/user_guide/database/connecting.html +++ /dev/null @@ -1,188 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Connecting to your Database : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Connecting -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - - -<h1>Connecting to your Database</h1> - -<p>There are two ways to connect to a database:</p> - -<h2>Automatically Connecting</h2> - -<p>The "auto connect" feature will load and instantiate the database class with every page load. -To enable "auto connecting", add the word <var>database</var> to the library array, as indicated in the following file:</p> - -<p><kbd>application/config/autoload.php</kbd></p> - -<h2>Manually Connecting</h2> - -<p>If only some of your pages require database connectivity you can manually connect to your database by adding this -line of code in any function where it is needed, or in your class constructor to make the database -available globally in that class.</p> - -<code>$this->load->database();</code> - -<p class="important">If the above function does <strong>not</strong> contain any information in the first parameter it will connect -to the group specified in your database config file. For most people, this is the preferred method of use.</p> - -<h3>Available Parameters</h3> - -<ol> - <li>The database connection values, passed either as an array or a DSN string.</li> - <li>TRUE/FALSE (boolean). Whether to return the connection ID (see Connecting to Multiple Databases below).</li> - <li>TRUE/FALSE (boolean). Whether to enable the Active Record class. Set to TRUE by default.</li> -</ol> - - -<h3>Manually Connecting to a Database</h3> - -<p>The first parameter of this function can <strong>optionally</strong> be used to specify a particular database group -from your config file, or you can even submit connection values for a database that is not specified in your config file. -Examples:</p> - -<p>To choose a specific group from your config file you can do this:</p> - -<code>$this->load->database('<samp>group_name</samp>');</code> - -<p>Where <samp>group_name</samp> is the name of the connection group from your config file.</p> - - -<p>To connect manually to a desired database you can pass an array of values:</p> - -<code>$config['hostname'] = "localhost";<br /> -$config['username'] = "myusername";<br /> -$config['password'] = "mypassword";<br /> -$config['database'] = "mydatabase";<br /> -$config['dbdriver'] = "mysql";<br /> -$config['dbprefix'] = "";<br /> -$config['pconnect'] = FALSE;<br /> -$config['db_debug'] = TRUE;<br /> -$config['cache_on'] = FALSE;<br /> -$config['cachedir'] = "";<br /> -$config['char_set'] = "utf8";<br /> -$config['dbcollat'] = "utf8_general_ci";<br /> -<br /> -$this->load->database(<samp>$config</samp>);</code> - -<p>For information on each of these values please see the <a href="configuration.html">configuration page</a>.</p> - -<p>Or you can submit your database values as a Data Source Name. DSNs must have this prototype:</p> - -<code>$dsn = 'dbdriver://username:password@hostname/database';<br /> -<br /> -$this->load->database(<samp>$dsn</samp>);</code> - -<p>To override default config values when connecting with a DSN string, add the config variables as a query string.</p> - -<code>$dsn = 'dbdriver://username:password@hostname/database?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir=/path/to/cache';<br /> -<br /> -$this->load->database(<samp>$dsn</samp>);</code> - -<h2>Connecting to Multiple Databases</h2> - -<p>If you need to connect to more than one database simultaneously you can do so as follows:</p> - - -<code>$DB1 = $this->load->database('group_one', TRUE);<br /> -$DB2 = $this->load->database('group_two', TRUE); -</code> - -<p>Note: Change the words "group_one" and "group_two" to the specific group names you are connecting to (or -you can pass the connection values as indicated above).</p> - -<p>By setting the second parameter to TRUE (boolean) the function will return the database object.</p> - -<div class="important"> -<p>When you connect this way, you will use your object name to issue commands rather than the syntax used throughout this guide. In other words, rather than issuing commands with:</p> - -<p>$this->db->query();<br />$this->db->result();<br /> etc...</p> - -<p>You will instead use:</p> - -<p>$DB1->query();<br />$DB1->result();<br /> etc...</p> - -</div> - -<h2>Reconnecting / Keeping the Connection Alive</h2> - -<p>If the database server's idle timeout is exceeded while you're doing some heavy PHP lifting (processing an image, for instance), you should consider pinging the server by using the <dfn>reconnect()</dfn> method before sending further queries, which can gracefully keep the connection alive or re-establish it.</p> - -<code>$this->db->reconnect();</code> - -<h2>Manually closing the Connection</h2> - -<p>While CodeIgniter intelligently takes care of closing your database connections, you can explicitly close the connection.</p> - -<code>$this->db->close();</code> -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="configuration.html">Database Configuration</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="queries.html">Queries</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/examples.html b/user_guide/database/examples.html deleted file mode 100644 index 58035557d..000000000 --- a/user_guide/database/examples.html +++ /dev/null @@ -1,217 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Database Quick Start : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Database Example Code -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - - -<h1>Database Quick Start: Example Code</h1> - -<p>The following page contains example code showing how the database class is used. For complete details please -read the individual pages describing each function.</p> - - -<h2>Initializing the Database Class</h2> - -<p>The following code loads and initializes the database class based on your <a href="configuration.html">configuration</a> settings:</p> - -<code>$this->load->database();</code> - -<p>Once loaded the class is ready to be used as described below.</p> - -<p>Note: If all your pages require database access you can connect automatically. See the <a href="connecting.html">connecting</a> page for details.</p> - - -<h2>Standard Query With Multiple Results (Object Version)</h2> - -<code>$query = $this->db->query('SELECT name, title, email FROM my_table');<br /> -<br /> -foreach ($query->result() as $row)<br /> -{<br /> - echo $row->title;<br /> - echo $row->name;<br /> - echo $row->email;<br /> -}<br /> -<br /> -echo 'Total Results: ' . $query->num_rows(); -</code> - -<p>The above <dfn>result()</dfn> function returns an array of <strong>objects</strong>. Example: $row->title</p> - - -<h2>Standard Query With Multiple Results (Array Version)</h2> - -<code>$query = $this->db->query('SELECT name, title, email FROM my_table');<br /> -<br /> -foreach ($query->result_array() as $row)<br /> -{<br /> - echo $row['title'];<br /> - echo $row['name'];<br /> - echo $row['email'];<br /> -}</code> - -<p>The above <dfn>result_array()</dfn> function returns an array of standard array indexes. Example: $row['title']</p> - - -<h2>Testing for Results</h2> - -<p>If you run queries that might <strong>not</strong> produce a result, you are encouraged to test for a result first -using the <dfn>num_rows()</dfn> function:</p> - -<code> -$query = $this->db->query("YOUR QUERY");<br /> -<br /> -if ($query->num_rows() > 0)<br /> -{<br /> - foreach ($query->result() as $row)<br /> - {<br /> - echo $row->title;<br /> - echo $row->name;<br /> - echo $row->body;<br /> - }<br /> -} -</code> - - - - -<h2>Standard Query With Single Result</h2> - -<code>$query = $this->db->query('SELECT name FROM my_table LIMIT 1');<br /> -<br /> -$row = $query->row();<br /> -echo $row->name;<br /> -</code> - -<p>The above <dfn>row()</dfn> function returns an <strong>object</strong>. Example: $row->name</p> - - -<h2>Standard Query With Single Result (Array version)</h2> - -<code>$query = $this->db->query('SELECT name FROM my_table LIMIT 1');<br /> -<br /> -$row = $query->row_array();<br /> -echo $row['name'];<br /> -</code> - -<p>The above <dfn>row_array()</dfn> function returns an <strong>array</strong>. Example: $row['name']</p> - - -<h2>Standard Insert</h2> - -<code> -$sql = "INSERT INTO mytable (title, name) <br /> - VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")";<br /> -<br /> -$this->db->query($sql);<br /> -<br /> -echo $this->db->affected_rows(); -</code> - - - - -<h2>Active Record Query</h2> - -<p>The <a href="active_record.html">Active Record Pattern</a> gives you a simplified means of retrieving data:</p> - -<code> -$query = $this->db->get('table_name');<br /> -<br /> -foreach ($query->result() as $row)<br /> -{<br /> - echo $row->title;<br /> -}</code> - -<p>The above <dfn>get()</dfn> function retrieves all the results from the supplied table. -The <a href="active_record.html">Active Record</a> class contains a full compliment of functions -for working with data.</p> - - -<h2>Active Record Insert</h2> - -<code> -$data = array(<br /> - 'title' => $title,<br /> - 'name' => $name,<br /> - 'date' => $date<br /> - );<br /> -<br /> -$this->db->insert('mytable', $data); -<br /><br /> -// Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')</code> - - - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="index.html">Database Class</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="configuration.html">Database Configuration</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/fields.html b/user_guide/database/fields.html deleted file mode 100644 index 56c9d9fdf..000000000 --- a/user_guide/database/fields.html +++ /dev/null @@ -1,163 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Field Data : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Field Names -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - - -<h1>Field Data</h1> - - -<h2>$this->db->list_fields()</h2> -<p>Returns an array containing the field names. This query can be called two ways:</p> - - -<p>1. You can supply the table name and call it from the <dfn>$this->db-></dfn> object:</p> - -<code> -$fields = $this->db->list_fields('table_name');<br /><br /> - -foreach ($fields as $field)<br /> -{<br /> - echo $field;<br /> -} -</code> - -<p>2. You can gather the field names associated with any query you run by calling the function -from your query result object:</p> - -<code> -$query = $this->db->query('SELECT * FROM some_table'); -<br /><br /> - -foreach ($query->list_fields() as $field)<br /> -{<br /> - echo $field;<br /> -} -</code> - - -<h2>$this->db->field_exists()</h2> - -<p>Sometimes it's helpful to know whether a particular field exists before performing an action. -Returns a boolean TRUE/FALSE. Usage example:</p> - -<code> -if ($this->db->field_exists('field_name', 'table_name'))<br /> -{<br /> - // some code...<br /> -} -</code> - -<p>Note: Replace <em>field_name</em> with the name of the column you are looking for, and replace -<em>table_name</em> with the name of the table you are looking for.</p> - - -<h2>$this->db->field_data()</h2> -<p>Returns an array of objects containing field information.</p> -<p>Sometimes it's helpful to gather the field names or other metadata, like the column type, max length, etc.</p> - - -<p class="important">Note: Not all databases provide meta-data.</p> - -<p>Usage example:</p> - -<code> -$fields = $this->db->field_data('table_name');<br /><br /> - -foreach ($fields as $field)<br /> -{<br /> - echo $field->name;<br /> - echo $field->type;<br /> - echo $field->max_length;<br /> - echo $field->primary_key;<br /> -} -</code> - -<p>If you have run a query already you can use the result object instead of supplying the table name:</p> - -<code> -$query = $this->db->query("YOUR QUERY");<br /> -$fields = $query->field_data(); -</code> - - -<p>The following data is available from this function if supported by your database:</p> - -<ul> -<li>name - column name</li> -<li>max_length - maximum length of the column</li> -<li>primary_key - 1 if the column is a primary key</li> -<li>type - the type of the column</li> -</ul> - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="table_data.html"> Table Data</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="call_function.html">Custom Function Calls</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/forge.html b/user_guide/database/forge.html deleted file mode 100644 index 2289e148e..000000000 --- a/user_guide/database/forge.html +++ /dev/null @@ -1,234 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Database Forge Class : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Database Forge Class -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - -<h1>Database Forge Class</h1> - -<p>The Database Forge Class contains functions that help you manage your database.</p> - -<h3>Table of Contents</h3> - -<ul> -<li><a href="#init">Initializing the Forge Class</a></li> -<li><a href="#create">Creating a Database</a></li> -<li><a href="#drop">Dropping a Database</a></li> -<li><a href="#add_field">Adding Fields</a></li> -<li><a href="#add_key">Adding Keys</a></li> -<li><a href="#create_table">Creating a Table</a></li> -<li><a href="#drop_table">Dropping a Table</a></li> -<li><a href="#rename_table">Renaming a Table</a></li> -<li><a href="#modifying_tables">Modifying a Table</a></li> -</ul> - - -<h2><a name="init"></a>Initializing the Forge Class</h2> - -<p class="important"><strong>Important:</strong> In order to initialize the Forge class, your database driver must -already be running, since the forge class relies on it.</p> - -<p>Load the Forge Class as follows:</p> - -<code>$this->load->dbforge()</code> - -<p>Once initialized you will access the functions using the <dfn>$this->dbforge</dfn> object:</p> - -<code>$this->dbforge->some_function()</code> -<h2><a name="create"></a>$this->dbforge->create_database('db_name')</h2> - -<p>Permits you to create the database specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p> - -<code>if ($this->dbforge->create_database('my_db'))<br /> -{<br /> - echo 'Database created!';<br /> -}</code> - - - - -<h2><a name="drop"></a>$this->dbforge->drop_database('db_name')</h2> - -<p>Permits you to drop the database specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p> - -<code>if ($this->dbforge->drop_database('my_db'))<br /> -{<br /> - echo 'Database deleted!';<br /> -}</code> - - -<h1>Creating and Dropping Tables</h1> -<p>There are several things you may wish to do when creating tables. Add fields, add keys to the table, alter columns. CodeIgniter provides a mechanism for this.</p> -<h2><a name="add_field" id="add_field"></a>Adding fields</h2> -<p>Fields are created via an associative array. Within the array you must include a 'type' key that relates to the datatype of the field. For example, INT, VARCHAR, TEXT, etc. Many datatypes (for example VARCHAR) also require a 'constraint' key.</p> -<p><code>$fields = array(<br /> - 'users' => array(<br /> - 'type' => 'VARCHAR',<br /> - 'constraint' => '100',<br /> - ),<br /> - );<br /> - <br /> -// will translate to "users VARCHAR(100)" when the field is added.</code></p> -<p>Additionally, the following key/values can be used:</p> -<ul> - <li>unsigned/true : to generate "UNSIGNED" in the field definition.</li> - <li>default/value : to generate a default value in the field definition.</li> - <li>null/true : to generate "NULL" in the field definition. Without this, the field will default to "NOT NULL".</li> - <li>auto_increment/true : generates an auto_increment flag on the field. Note that the field type must be a type that supports this, such as integer.</li> - </ul> -<p><code>$fields = array(<br /> - 'blog_id' => array(<br /> - 'type' => 'INT',<br /> - 'constraint' => 5, <br /> - 'unsigned' => TRUE,<br /> - 'auto_increment' => TRUE<br /> - ),<br /> - 'blog_title' => array(<br /> - 'type' => 'VARCHAR',<br /> - 'constraint' => '100',<br /> - ),<br /> - 'blog_author' => array(<br /> - 'type' =>'VARCHAR',<br /> - 'constraint' => '100',<br /> - 'default' => 'King of Town',<br /> - ),<br /> - 'blog_description' => array(<br /> - 'type' => 'TEXT',<br /> - 'null' => TRUE,<br /> - ),<br /> - );<br /> -</code></p> -<p>After the fields have been defined, they can be added using <dfn>$this->dbforge->add_field($fields);</dfn> followed by a call to the <dfn>create_table()</dfn> function.</p> -<h3>$this->dbforge->add_field()</h3> -<p>The add fields function will accept the above array.</p> -<h3>Passing strings as fields</h3> -<p>If you know exactly how you want a field to be created, you can pass the string into the field definitions with add_field()</p> -<p><code>$this->dbforge->add_field("label varchar(100) NOT NULL DEFAULT 'default label'");</code></p> -<p class="important">Note: Multiple calls to <dfn>add_field()</dfn> are cumulative.</p> -<h3>Creating an id field</h3> -<p>There is a special exception for creating id fields. A field with type id will automatically be assinged as an INT(9) auto_incrementing Primary Key.</p> -<p><code>$this->dbforge->add_field('id');<br /> - // gives id INT(9) NOT NULL AUTO_INCREMENT</code></p> -<h2><a name="add_key" id="add_key"></a>Adding Keys</h2> -<p>Generally speaking, you'll want your table to have Keys. This is accomplished with <dfn>$this->dbforge->add_key('field')</dfn>. An optional second parameter set to TRUE will make it a primary key. Note that <dfn>add_key()</dfn> must be followed by a call to <dfn>create_table()</dfn>.</p> -<p>Multiple column non-primary keys must be sent as an array. Sample output below is for MySQL.</p> -<p><code>$this->dbforge->add_key('blog_id', TRUE);<br /> - // gives PRIMARY KEY `blog_id` (`blog_id`)<br /> - <br /> - $this->dbforge->add_key('blog_id', TRUE);<br /> - $this->dbforge->add_key('site_id', TRUE);<br /> - // gives PRIMARY KEY `blog_id_site_id` (`blog_id`, `site_id`)<br /> - <br /> - $this->dbforge->add_key('blog_name');<br /> - // gives KEY `blog_name` (`blog_name`)<br /> - <br /> - $this->dbforge->add_key(array('blog_name', 'blog_label'));<br /> - // gives KEY `blog_name_blog_label` (`blog_name`, `blog_label`)</code></p> -<h2><a name="create_table" id="create_table"></a>Creating a table</h2> -<p>After fields and keys have been declared, you can create a new table with</p> -<p><code>$this->dbforge->create_table('table_name');<br /> -// gives CREATE TABLE table_name</code></p> -<p>An optional second parameter set to TRUE adds an "IF NOT EXISTS" clause into the definition</p> -<p><code>$this->dbforge->create_table('table_name', TRUE);<br /> -// gives CREATE TABLE IF NOT EXISTS table_name</code></p> -<h2><a name="drop_table" id="drop_table"></a>Dropping a table</h2> -<p>Executes a DROP TABLE sql</p> -<p><code>$this->dbforge->drop_table('table_name');<br /> - // gives DROP TABLE IF EXISTS table_name</code></p> -<h2><a name="rename_table" id="rename_table"></a>Renaming a table</h2> -<p>Executes a TABLE rename</p> -<p><code>$this->dbforge->rename_table('old_table_name', 'new_table_name');<br /> - // gives ALTER TABLE old_table_name RENAME TO new_table_name</code></p> -<h1><a name="modifying_tables" id="modifying_tables"></a>Modifying Tables</h1> -<h2>$this->dbforge->add_column()</h2> -<p>The add_column() function is used to modify an existing table. It accepts the same field array as above, and can be used for an unlimited number of additional fields.</p> -<p><code>$fields = array(<br /> - 'preferences' => array('type' => 'TEXT')<br /> -);<br /> -$this->dbforge->add_column('table_name', $fields);<br /> -<br /> -// gives ALTER TABLE table_name ADD preferences TEXT</code></p> -<h2>$this->dbforge->drop_column()</h2> -<p>Used to remove a column from a table. </p> -<p><code>$this->dbforge->drop_column('table_name', 'column_to_drop');</code></p> -<h2>$this->dbforge->modify_column()</h2> -<p>The usage of this function is identical to add_column(), except it alters an existing column rather than adding a new one. In order to change the name you can add a "name" key into the field defining array.</p> -<p><code>$fields = array(<br /> - 'old_name' => array(<br /> - 'name' => 'new_name',<br /> - 'type' => 'TEXT',<br /> - ),<br /> -);<br /> -$this->dbforge->modify_column('table_name', $fields);<br /> - <br /> - // gives ALTER TABLE table_name CHANGE old_name new_name TEXT </code></p> -<p> </p> -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="caching.html">DB Caching Class</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="utilities.html">Database Utilities Class</a><a href="../libraries/email.html"></a></p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/helpers.html b/user_guide/database/helpers.html deleted file mode 100644 index 82f5c1d21..000000000 --- a/user_guide/database/helpers.html +++ /dev/null @@ -1,151 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Query Helper Functions : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Query Helpers -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - - -<h1>Query Helper Functions</h1> - - -<h2>$this->db->insert_id()</h2> -<p>The insert ID number when performing database inserts.</p> - -<h2>$this->db->affected_rows()</h2> -<p>Displays the number of affected rows, when doing "write" type queries (insert, update, etc.).</p> -<p>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.</p> - - -<h2>$this->db->count_all();</h2> -<p>Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example:</p> -<code>echo $this->db->count_all('<var>my_table</var>');<br /> -<br /> -// Produces an integer, like 25 -</code> - - -<h2>$this->db->platform()</h2> -<p>Outputs the database platform you are running (MySQL, MS SQL, Postgres, etc...):</p> -<code>echo $this->db->platform();</code> - - -<h2>$this->db->version()</h2> -<p>Outputs the database version you are running:</p> -<code>echo $this->db->version();</code> - - -<h2>$this->db->last_query();</h2> -<p>Returns the last query that was run (the query string, not the result). Example:</p> - -<code>$str = $this->db->last_query();<br /> -<br /> -// Produces: SELECT * FROM sometable.... -</code> - - -<p>The following two functions help simplify the process of writing database INSERTs and UPDATEs.</p> - - -<h2>$this->db->insert_string(); </h2> -<p>This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example:</p> - -<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br /> -<br /> -$str = $this->db->insert_string('table_name', $data); -</code> - -<p>The first parameter is the table name, the second is an associative array with the data to be inserted. The above example produces:</p> -<code>INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com')</code> - -<p class="important">Note: Values are automatically escaped, producing safer queries.</p> - - - -<h2>$this->db->update_string(); </h2> -<p>This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:</p> - -<code>$data = array('name' => $name, 'email' => $email, 'url' => $url);<br /> -<br /> -$where = "author_id = 1 AND status = 'active'"; -<br /><br /> -$str = $this->db->update_string('table_name', $data, $where); -</code> - -<p>The first parameter is the table name, the second is an associative array with the data to be updated, and the third parameter is the "where" clause. The above example produces:</p> -<code> UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active'</code> - -<p class="important">Note: Values are automatically escaped, producing safer queries.</p> - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="results.html">Query Results</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="active_record.html">Active Record Pattern</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/index.html b/user_guide/database/index.html deleted file mode 100644 index 8a957ecef..000000000 --- a/user_guide/database/index.html +++ /dev/null @@ -1,99 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>The Database Class : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -Database Library -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - - -<h1>The Database Class</h1> - -<p>CodeIgniter comes with a full-featured and very fast abstracted database class that supports both traditional -structures and Active Record patterns. The database functions offer clear, simple syntax.</p> - - <ul> - <li><a href="examples.html">Quick Start: Usage Examples</a></li> - <li><a href="configuration.html">Database Configuration</a></li> - <li><a href="connecting.html">Connecting to a Database</a></li> - <li><a href="queries.html">Running Queries</a></li> - <li><a href="results.html">Generating Query Results</a></li> - <li><a href="helpers.html">Query Helper Functions</a></li> - <li><a href="active_record.html">Active Record Class</a></li> - <li><a href="transactions.html">Transactions</a></li> - <li><a href="table_data.html">Table MetaData</a></li> - <li><a href="fields.html">Field MetaData</a></li> - <li><a href="call_function.html">Custom Function Calls</a></li> - <li><a href="caching.html">Query Caching</a></li> - <li><a href="forge.html">Database manipulation with Database Forge</a></li> - <li><a href="utilities.html">Database Utilities Class</a></li> - </ul> - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="../libraries/caching.html">Caching Class</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="examples.html">Quick Start: Usage Examples</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/queries.html b/user_guide/database/queries.html deleted file mode 100644 index 3152997ca..000000000 --- a/user_guide/database/queries.html +++ /dev/null @@ -1,158 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Queries : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Queries -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - - -<h1>Queries</h1> - -<h2>$this->db->query();</h2> - -<p>To submit a query, use the following function:</p> - -<code>$this->db->query('YOUR QUERY HERE');</code> - -<p>The <dfn>query()</dfn> function returns a database result <strong>object</strong> when "read" type queries are run, -which you can use to <a href="results.html">show your results</a>. 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:</p> - -<code><var>$query</var> = $this->db->query('YOUR QUERY HERE');</code> - -<h2>$this->db->simple_query();</h2> - -<p>This is a simplified version of the <dfn>$this->db->query()</dfn> 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.</p> - - -<h1>Working with Database prefixes manually</h1> -<p>If you have configured a database prefix and would like to prepend it to a table name for use in a native SQL query for example, then you can use the following:</p> -<p><code>$this->db->dbprefix('tablename');<br /> -// outputs prefix_tablename</code></p> - -<p>If for any reason you would like to change the prefix programatically without needing to create a new connection, you can use this method:</p> -<p><code>$this->db->set_dbprefix('newprefix');<br /><br /> -$this->db->dbprefix('tablename');<br /> -// outputs newprefix_tablename</code></p> - - -<h1>Protecting identifiers</h1> -<p>In many databases it is advisable to protect table and field names - for example with backticks in MySQL. <strong>Active Record queries are automatically protected</strong>, however if you need to manually protect an identifier you can use:</p> -<p><code>$this->db->protect_identifiers('table_name');</code></p> - -<p>This function will also add a table prefix to your table, assuming you have a prefix specified in your database config file. To enable the prefixing set <kbd>TRUE</kbd> (boolen) via the second parameter:</p> -<p><code>$this->db->protect_identifiers('table_name', <kbd>TRUE</kbd>);</code></p> - - -<h1>Escaping Queries</h1> -<p>It's a very good security practice to escape your data before submitting it into your database. -CodeIgniter has three methods that help you do this:</p> - -<ol> -<li><strong>$this->db->escape()</strong> This function determines the data type so that it -can escape only string data. It also automatically adds single quotes around the data so you don't have to: - -<code>$sql = "INSERT INTO table (title) VALUES(".$this->db->escape($title).")";</code></li> - -<li><strong>$this->db->escape_str()</strong> This function escapes the data passed to it, regardless of type. -Most of the time you'll use the above function rather than this one. Use the function like this: - -<code>$sql = "INSERT INTO table (title) VALUES('".$this->db->escape_str($title)."')";</code></li> - -<li><strong>$this->db->escape_like_str()</strong> This method should be used when strings are to be used in LIKE -conditions so that LIKE wildcards ('%', '_') in the string are also properly escaped. - -<code>$search = '20% raise';<br /> -$sql = "SELECT id FROM table WHERE column LIKE '%".$this->db->escape_like_str($search)."%'";</code></li> - -</ol> - - -<h1>Query Bindings</h1> - - -<p>Bindings enable you to simplify your query syntax by letting the system put the queries together for you. Consider the following example:</p> - -<code> -$sql = "SELECT * FROM some_table WHERE id = <var>?</var> AND status = <var>?</var> AND author = <var>?</var>"; -<br /><br /> -$this->db->query($sql, array(3, 'live', 'Rick')); -</code> - -<p>The question marks in the query are automatically replaced with the values in the array in the second parameter of the query function.</p> -<p class="important">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.</p> - - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="connecting.html">Connecting to your Database</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="results.html">Query Results</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/results.html b/user_guide/database/results.html deleted file mode 100644 index 0baf992fb..000000000 --- a/user_guide/database/results.html +++ /dev/null @@ -1,259 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Generating Query Results : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Query Results -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - - - -<h1>Generating Query Results</h1> - - -<p>There are several ways to generate query results:</p> - - <h2>result()</h2> - - <p>This function returns the query result as an array of <strong>objects</strong>, or <strong>an empty array</strong> on failure. - - Typically you'll use this in a foreach loop, like this:</p> - - <code> - $query = $this->db->query("YOUR QUERY");<br /> - <br /> - foreach ($query->result() as $row)<br /> - {<br /> - echo $row->title;<br /> - echo $row->name;<br /> - echo $row->body;<br /> - }</code> - - <p>The above <dfn>function</dfn> is an alias of <dfn>result_object()</dfn>.</p> - - <p>If you run queries that might <strong>not</strong> produce a result, you are encouraged to test the result first:</p> - - <code> - $query = $this->db->query("YOUR QUERY");<br /> - <br /> - if ($query->num_rows() > 0)<br /> - {<br /> - foreach ($query->result() as $row)<br /> - {<br /> - echo $row->title;<br /> - echo $row->name;<br /> - echo $row->body;<br /> - }<br /> - } - </code> - - <p>You can also pass a string to result() which represents a class to instantiate for each result object (note: this class must be loaded)</p> - - <code> - $query = $this->db->query("SELECT * FROM users;");<br /> - <br /> - foreach ($query->result('User') as $row)<br /> - {<br /> - echo $row->name; // call attributes<br /> - echo $row->reverse_name(); // or methods defined on the 'User' class<br /> - } - </code> - - <h2>result_array()</h2> - - <p>This function returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this:</p> - <code> - $query = $this->db->query("YOUR QUERY");<br /> - <br /> - foreach ($query->result_array() as $row)<br /> - {<br /> - echo $row['title'];<br /> - echo $row['name'];<br /> - echo $row['body'];<br /> - }</code> - - - <h2>row()</h2> - - <p>This function returns a single result row. If your query has more than one row, it returns only the first row. - The result is returned as an <strong>object</strong>. Here's a usage example:</p> - <code> - $query = $this->db->query("YOUR QUERY");<br /> - <br /> - if ($query->num_rows() > 0)<br /> - {<br /> - $row = $query->row(); - <br /><br /> - echo $row->title;<br /> - echo $row->name;<br /> - echo $row->body;<br /> - } - </code> - - <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:</p> - - <code>$row = $query->row(<dfn>5</dfn>);</code> - - <p>You can also add a second String parameter, which is the name of a class to instantiate the row with:</p> - - <code> - $query = $this->db->query("SELECT * FROM users LIMIT 1;");<br /> - <br /> - $query->row(0, 'User')<br /> - echo $row->name; // call attributes<br /> - echo $row->reverse_name(); // or methods defined on the 'User' class<br /> - </code> - - <h2>row_array()</h2> - - <p>Identical to the above <var>row()</var> function, except it returns an array. Example:</p> - - <code> - $query = $this->db->query("YOUR QUERY");<br /> - <br /> - if ($query->num_rows() > 0)<br /> - {<br /> - $row = $query->row_array(); - <br /><br /> - echo $row['title'];<br /> - echo $row['name'];<br /> - echo $row['body'];<br /> - } - </code> - - - <p>If you want a specific row returned you can submit the row number as a digit in the first parameter:</p> - - <code>$row = $query->row_array(<dfn>5</dfn>);</code> - - - <p>In addition, you can walk forward/backwards/first/last through your results using these variations:</p> - -<p> - <strong>$row = $query->first_row()</strong><br /> - <strong>$row = $query->last_row()</strong><br /> - <strong>$row = $query->next_row()</strong><br /> - <strong>$row = $query->previous_row()</strong> -</p> - -<p>By default they return an object unless you put the word "array" in the parameter:</p> - -<p> - <strong>$row = $query->first_row('array')</strong><br /> - <strong>$row = $query->last_row('array')</strong><br /> - <strong>$row = $query->next_row('array')</strong><br /> - <strong>$row = $query->previous_row('array')</strong> -</p> - - - -<h1>Result Helper Functions</h1> - - -<h2>$query->num_rows()</h2> -<p>The number of rows returned by the query. Note: In this example, <dfn>$query</dfn> is the variable that the query result object is assigned to:</p> - -<code>$query = $this->db->query('SELECT * FROM my_table');<br /><br /> -echo $query->num_rows(); -</code> - -<h2>$query->num_fields()</h2> -<p>The number of FIELDS (columns) returned by the query. Make sure to call the function using your query result object:</p> - -<code>$query = $this->db->query('SELECT * FROM my_table');<br /><br /> -echo $query->num_fields(); -</code> - - - -<h2>$query->free_result()</h2> -<p>It frees the memory associated with the result and deletes the result resource ID. Normally PHP frees its memory automatically at the end of script -execution. However, if you are running a lot of queries in a particular script you might want to free the result after each query result has been -generated in order to cut down on memory consumptions. Example: -</p> - -<code>$query = $this->db->query('SELECT title FROM my_table');<br /><br /> -foreach ($query->result() as $row)<br /> -{<br /> - echo $row->title;<br /> -}<br /> -$query->free_result(); // The $query result object will no longer be available<br /> -<br /> -$query2 = $this->db->query('SELECT name FROM some_table');<br /><br /> -$row = $query2->row();<br /> -echo $row->name;<br /> -$query2->free_result(); // The $query2 result object will no longer be available -</code> - - - - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="queries.html">Queries</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="helpers.html">Query Helper Functions</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html> diff --git a/user_guide/database/table_data.html b/user_guide/database/table_data.html deleted file mode 100644 index dc5b54198..000000000 --- a/user_guide/database/table_data.html +++ /dev/null @@ -1,113 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Table Data : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Table Data -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - - - -<h1>Table Data</h1> - -<p>These functions let you fetch table information.</p> - -<h2>$this->db->list_tables();</h2> - -<p>Returns an array containing the names of all the tables in the database you are currently connected to. Example:</p> - -<code>$tables = $this->db->list_tables();<br /> -<br /> -foreach ($tables as $table)<br /> -{<br /> - echo $table;<br /> -} -</code> - - -<h2>$this->db->table_exists();</h2> - -<p>Sometimes it's helpful to know whether a particular table exists before running an operation on it. -Returns a boolean TRUE/FALSE. Usage example:</p> - -<code> -if ($this->db->table_exists('table_name'))<br /> -{<br /> - // some code...<br /> -} -</code> - -<p>Note: Replace <em>table_name</em> with the name of the table you are looking for.</p> - - - - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="transactions.html"> Transactions</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="fields.html"> Field Metadata</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/transactions.html b/user_guide/database/transactions.html deleted file mode 100644 index dd5f73ed1..000000000 --- a/user_guide/database/transactions.html +++ /dev/null @@ -1,200 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Transactions : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Transactions -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - - -<h1>Transactions</h1> - -<p>CodeIgniter's database abstraction allows you to use <dfn>transactions</dfn> with databases that support transaction-safe table types. In MySQL, you'll need -to be running InnoDB or BDB table types rather than the more common MyISAM. Most other database platforms support transactions natively.</p> - -<p>If you are not familiar with -transactions we recommend you find a good online resource to learn about them for your particular database. The information below assumes you -have a basic understanding of transactions. -</p> - -<h2>CodeIgniter's Approach to Transactions</h2> - -<p>CodeIgniter utilizes an approach to transactions that is very similar to the process used by the popular database class ADODB. We've chosen that approach -because it greatly simplifies the process of running transactions. In most cases all that is required are two lines of code.</p> - -<p>Traditionally, transactions have required a fair amount of work to implement since they demand that you to keep track of your queries -and determine whether to <dfn>commit</dfn> or <dfn>rollback</dfn> based on the success or failure of your queries. This is particularly cumbersome with -nested queries. In contrast, -we've implemented a smart transaction system that does all this for you automatically (you can also manage your transactions manually if you choose to, -but there's really no benefit).</p> - -<h2>Running Transactions</h2> - -<p>To run your queries using transactions you will use the <dfn>$this->db->trans_start()</dfn> and <dfn>$this->db->trans_complete()</dfn> functions as follows:</p> - -<code> -<kbd>$this->db->trans_start();</kbd><br /> -$this->db->query('AN SQL QUERY...');<br /> -$this->db->query('ANOTHER QUERY...');<br /> -$this->db->query('AND YET ANOTHER QUERY...');<br /> -<kbd>$this->db->trans_complete();</kbd> -</code> - -<p>You can run as many queries as you want between the start/complete functions and they will all be committed or rolled back based on success or failure -of any given query.</p> - - -<h2>Strict Mode</h2> - -<p>By default CodeIgniter runs all transactions in <dfn>Strict Mode</dfn>. When strict mode is enabled, if you are running multiple groups of -transactions, if one group fails all groups will be rolled back. If strict mode is disabled, each group is treated independently, meaning -a failure of one group will not affect any others.</p> - -<p>Strict Mode can be disabled as follows:</p> - -<code>$this->db->trans_strict(FALSE);</code> - - -<h2>Managing Errors</h2> - -<p>If you have error reporting enabled in your <dfn>config/database.php</dfn> file you'll see a standard error message if the commit was unsuccessful. If debugging is turned off, you can -manage your own errors like this:</p> - -<code> -$this->db->trans_start();<br /> -$this->db->query('AN SQL QUERY...');<br /> -$this->db->query('ANOTHER QUERY...');<br /> -$this->db->trans_complete();<br /> -<br /> -if (<kbd>$this->db->trans_status()</kbd> === FALSE)<br /> -{<br /> - // generate an error... or use the log_message() function to log your error<br /> -} -</code> - - -<h2>Enabling Transactions</h2> - -<p>Transactions are enabled automatically the moment you use <dfn>$this->db->trans_start()</dfn>. If you would like to disable transactions you -can do so using <dfn>$this->db->trans_off()</dfn>:</p> - -<code> -<kbd>$this->db->trans_off()</kbd><br /><br /> - -$this->db->trans_start();<br /> -$this->db->query('AN SQL QUERY...');<br /> -$this->db->trans_complete(); -</code> - -<p class="important">When transactions are disabled, your queries will be auto-commited, just as they are when running queries without transactions.</p> - - -<h2>Test Mode</h2> - -<p>You can optionally put the transaction system into "test mode", which will cause your queries to be rolled back -- even if the queries produce a valid result. -To use test mode simply set the first parameter in the <dfn>$this->db->trans_start()</dfn> function to <samp>TRUE</samp>:</p> - -<code> -$this->db->trans_start(<samp>TRUE</samp>); // Query will be rolled back<br /> -$this->db->query('AN SQL QUERY...');<br /> -$this->db->trans_complete(); -</code> - - -<h2>Running Transactions Manually</h2> - -<p>If you would like to run transactions manually you can do so as follows:</p> - -<code> -$this->db->trans_begin();<br /><br /> - -$this->db->query('AN SQL QUERY...');<br /> -$this->db->query('ANOTHER QUERY...');<br /> -$this->db->query('AND YET ANOTHER QUERY...');<br /> - -<br /> - -if ($this->db->trans_status() === FALSE)<br /> -{<br /> - $this->db->trans_rollback();<br /> -}<br /> -else<br /> -{<br /> - $this->db->trans_commit();<br /> -}<br /> -</code> - -<p class="important"><strong>Note:</strong> Make sure to use <kbd>$this->db->trans_begin()</kbd> when running manual transactions, <strong>NOT</strong> -<dfn>$this->db->trans_start()</dfn>.</p> - - - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="fields.html">Field MetaData</a> · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="table_data.html">Table Metadata</a> -</p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file diff --git a/user_guide/database/utilities.html b/user_guide/database/utilities.html deleted file mode 100644 index 7c30070f6..000000000 --- a/user_guide/database/utilities.html +++ /dev/null @@ -1,314 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head> - -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>Database Utility Class : CodeIgniter User Guide</title> - -<style type='text/css' media='all'>@import url('../userguide.css');</style> -<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' /> - -<script type="text/javascript" src="../nav/nav.js"></script> -<script type="text/javascript" src="../nav/prototype.lite.js"></script> -<script type="text/javascript" src="../nav/moo.fx.js"></script> -<script type="text/javascript" src="../nav/user_guide_menu.js"></script> - -<meta http-equiv='expires' content='-1' /> -<meta http-equiv= 'pragma' content='no-cache' /> -<meta name='robots' content='all' /> -<meta name='author' content='ExpressionEngine Dev Team' /> -<meta name='description' content='CodeIgniter User Guide' /> - -</head> -<body> - -<!-- START NAVIGATION --> -<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div> -<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div> -<div id="masthead"> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td><h1>CodeIgniter User Guide Version 2.1.0</h1></td> -<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td> -</tr> -</table> -</div> -<!-- END NAVIGATION --> - - -<!-- START BREADCRUMB --> -<table cellpadding="0" cellspacing="0" border="0" style="width:100%"> -<tr> -<td id="breadcrumb"> -<a href="http://codeigniter.com/">CodeIgniter Home</a> › -<a href="../index.html">User Guide Home</a> › -<a href="index.html">Database Library</a> › -Database Utility Class -</td> -<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td> -</tr> -</table> -<!-- END BREADCRUMB --> - - -<br clear="all" /> - - -<!-- START CONTENT --> -<div id="content"> - -<h1>Database Utility Class</h1> - -<p>The Database Utility Class contains functions that help you manage your database.</p> - -<h3>Table of Contents</h3> - -<ul> -<li><a href="#init">Initializing the Utility Class</a></li> -<li><a href="#list">Listing your Databases</a></li> -<li><a href="#exists">Checking for a specific Database</a></li> -<li><a href="#opttb">Optimizing your Tables</a></li> -<li><a href="#repair">Repairing your Databases</a></li> -<li><a href="#optdb">Optimizing your Database</a></li> -<li><a href="#csv">CSV Files from a Database Result</a></li> -<li><a href="#xml">XML Files from a Database Result</a></li> -<li><a href="#backup">Backing up your Database</a></li> -</ul> - - - -<h2><a name="init"></a>Initializing the Utility Class</h2> - -<p class="important"><strong>Important:</strong> In order to initialize the Utility class, your database driver must -already be running, since the utilities class relies on it.</p> - -<p>Load the Utility Class as follows:</p> - -<code>$this->load->dbutil()</code> - -<p>Once initialized you will access the functions using the <dfn>$this->dbutil</dfn> object:</p> - -<code>$this->dbutil->some_function()</code> - -<h2><a name="list"></a>$this->dbutil->list_databases()</h2> -<p>Returns an array of database names:</p> - -<code> -$dbs = $this->dbutil->list_databases();<br /> -<br /> -foreach ($dbs as $db)<br /> -{<br /> - echo $db;<br /> -}</code> - - -<h2><a name="exists"></a>$this->dbutil->database_exists();</h2> - -<p>Sometimes it's helpful to know whether a particular database exists. -Returns a boolean TRUE/FALSE. Usage example:</p> - -<code> -if ($this->dbutil->database_exists('database_name'))<br /> -{<br /> - // some code...<br /> -} -</code> - -<p>Note: Replace <em>database_name</em> with the name of the table you are looking for. This function is case sensitive.</p> - - - -<h2><a name="opttb"></a>$this->dbutil->optimize_table('table_name');</h2> - -<p class="important"><strong>Note:</strong> This features is only available for MySQL/MySQLi databases.</p> - - -<p>Permits you to optimize a table using the table name specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p> - -<code> -if ($this->dbutil->optimize_table('table_name'))<br /> -{<br /> - echo 'Success!';<br /> -} -</code> - -<p><strong>Note:</strong> Not all database platforms support table optimization.</p> - - -<h2><a name="repair"></a>$this->dbutil->repair_table('table_name');</h2> - -<p class="important"><strong>Note:</strong> This features is only available for MySQL/MySQLi databases.</p> - - -<p>Permits you to repair a table using the table name specified in the first parameter. Returns TRUE/FALSE based on success or failure:</p> - -<code> -if ($this->dbutil->repair_table('table_name'))<br /> -{<br /> - echo 'Success!';<br /> -} -</code> - -<p><strong>Note:</strong> Not all database platforms support table repairs.</p> - - -<h2><a name="optdb"></a>$this->dbutil->optimize_database();</h2> - -<p class="important"><strong>Note:</strong> This features is only available for MySQL/MySQLi databases.</p> - -<p>Permits you to optimize the database your DB class is currently connected to. Returns an array containing the DB status messages or FALSE on failure.</p> - -<code> -$result = $this->dbutil->optimize_database();<br /> -<br /> -if ($result !== FALSE)<br /> -{<br /> - print_r($result);<br /> -} -</code> - -<p><strong>Note:</strong> Not all database platforms support table optimization.</p> - - -<h2><a name="csv"></a>$this->dbutil->csv_from_result($db_result)</h2> - -<p>Permits you to generate a CSV file from a query result. The first parameter of the function must contain the result object from your query. -Example:</p> - -<code> -$this->load->dbutil();<br /> -<br /> -$query = $this->db->query("SELECT * FROM mytable");<br /> -<br /> -echo $this->dbutil->csv_from_result($query); -</code> - -<p>The second and third parameters allows you to -set the delimiter and newline character. By default tabs are used as the delimiter and "\n" is used as a new line. Example:</p> - -<code> -$delimiter = ",";<br /> -$newline = "\r\n";<br /> -<br /> -echo $this->dbutil->csv_from_result($query, $delimiter, $newline); -</code> - -<p><strong>Important:</strong> This function will NOT write the CSV file for you. It simply creates the CSV layout. -If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p> - - -<h2><a name="xml"></a>$this->dbutil->xml_from_result($db_result)</h2> - -<p>Permits you to generate an XML file from a query result. The first parameter expects a query result object, the second -may contain an optional array of config parameters. Example:</p> - -<code> -$this->load->dbutil();<br /> -<br /> -$query = $this->db->query("SELECT * FROM mytable");<br /> -<br /> -$config = array (<br /> - 'root' => 'root',<br /> - 'element' => 'element', <br /> - 'newline' => "\n", <br /> - 'tab' => "\t"<br /> - );<br /> -<br /> -echo $this->dbutil->xml_from_result($query, $config); -</code> - -<p><strong>Important:</strong> This function will NOT write the XML file for you. It simply creates the XML layout. -If you need to write the file use the <a href="../helpers/file_helper.html">File Helper</a>.</p> - - -<h2><a name="backup"></a>$this->dbutil->backup()</h2> - -<p>Permits you to backup your full database or individual tables. The backup data can be compressed in either Zip or Gzip format.</p> - -<p class="important"><strong>Note:</strong> This features is only available for MySQL databases.</p> - -<p>Note: Due to the limited execution time and memory available to PHP, backing up very large -databases may not be possible. If your database is very large you might need to backup directly from your SQL server -via the command line, or have your server admin do it for you if you do not have root privileges.</p> - -<h3>Usage Example</h3> - -<code> -<dfn>// Load the DB utility class</dfn><br /> -$this->load->dbutil();<br /><br /> - -<dfn>// Backup your entire database and assign it to a variable</dfn><br /> -$backup =& $this->dbutil->backup(); - -<br /><br /> -<dfn>// Load the file helper and write the file to your server</dfn><br /> -$this->load->helper('file');<br /> -write_file('/path/to/mybackup.gz', $backup); - -<br /><br /> -<dfn>// Load the download helper and send the file to your desktop</dfn><br /> -$this->load->helper('download');<br /> -force_download('mybackup.gz', $backup); -</code> - -<h3>Setting Backup Preferences</h3> - -<p>Backup preferences are set by submitting an array of values to the first parameter of the backup function. Example:</p> - -<code>$prefs = array(<br /> - 'tables' => array('table1', 'table2'), // Array of tables to backup.<br /> - 'ignore' => array(), // List of tables to omit from the backup<br /> - 'format' => 'txt', // gzip, zip, txt<br /> - 'filename' => 'mybackup.sql', // File name - NEEDED ONLY WITH ZIP FILES<br /> - 'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file<br /> - 'add_insert' => TRUE, // Whether to add INSERT data to backup file<br /> - 'newline' => "\n" // Newline character used in backup file<br /> - );<br /> -<br /> -$this->dbutil->backup($prefs); -</code> - - -<h3>Description of Backup Preferences</h3> - -<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder"> -<tr> -<th>Preference</th> -<th>Default Value</th> -<th>Options</th> -<th>Description</th> -</tr><tr> -<td class="td"><strong>tables</strong></td><td class="td">empty array</td><td class="td">None</td><td class="td">An array of tables you want backed up. If left blank all tables will be exported.</td> -</tr><tr> -<td class="td"><strong>ignore</strong></td><td class="td">empty array</td><td class="td">None</td><td class="td">An array of tables you want the backup routine to ignore.</td> -</tr><tr> -<td class="td"><strong>format</strong></td><td class="td">gzip</td><td class="td">gzip, zip, txt</td><td class="td">The file format of the export file.</td> -</tr><tr> -<td class="td"><strong>filename</strong></td><td class="td">the current date/time</td><td class="td">None</td><td class="td">The name of the backed-up file. The name is needed only if you are using zip compression.</td> -</tr><tr> -<td class="td"><strong>add_drop</strong></td><td class="td">TRUE</td><td class="td">TRUE/FALSE</td><td class="td">Whether to include DROP TABLE statements in your SQL export file.</td> -</tr><tr> -<td class="td"><strong>add_insert</strong></td><td class="td">TRUE</td><td class="td">TRUE/FALSE</td><td class="td">Whether to include INSERT statements in your SQL export file.</td> -</tr><tr> -<td class="td"><strong>newline</strong></td><td class="td">"\n"</td><td class="td">"\n", "\r", "\r\n"</td><td class="td">Type of newline to use in your SQL export file.</td> - -</tr> -</table> - - -</div> -<!-- END CONTENT --> - - -<div id="footer"> -<p> -Previous Topic: <a href="forge.html">DB Forge Class</a> - · -<a href="#top">Top of Page</a> · -<a href="../index.html">User Guide Home</a> · -Next Topic: <a href="../libraries/javascript.html">Javascript Class</a></p> -<p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006 - 2011 · <a href="http://ellislab.com/">EllisLab, Inc.</a></p> -</div> - -</body> -</html>
\ No newline at end of file |