1 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en">
3 <title>CarbonPHP Documentation
</title>
4 <style type=
"text/css">
11 font-family: "Lucida Sans Unicode", "Lucida Grande";
33 margin: 10px 0 10px 0;
34 list-style-position: inside
;
42 border: 1px dotted
#444;
50 border: 1px solid
#ccc;
61 <h1>Database Queries
</h1>
63 <p>These outline the ways in which you can submit queries to your database.
</p>
67 <h1>$this-
>db-
>query()
</h1>
69 <p>To submit a query you can use the following method.
</p>
72 $this-
>db-
>query('SQL QUERY HERE');
75 <p>The
<b>query()
</b> method returns a database result object when 'read' type queries run, which you can
76 then use to show database results. When a 'write' type of query is used it will simply return a true or false
77 depending on success or failure. When retrieving data you will typically assign the return value to a
81 $query = $this-
>db-
>query('SQL QUERY HERE');
86 <h1>$this-
>db-
>simple_query()
</h1>
88 <p>This is a simplified version of the
<b>query()
</b> method. It will only return true or false. It will
89 not return a database result set, nor does it set the query timer, or compile bind data, or store your query
90 for debugging. It simply submits a query.
</p>
94 <h1>Adding Database Prefixes Manually
</h1>
96 <p>If you have configured a database prefix and would like to add it in manually, you can use the following.
</p>
99 $this-
>db-
>dbprefix('tablename');
100 // outputs prefix_tablename
104 <div class=
"content">
105 <h1>Protecting Identifiers
</h1>
107 <p>In many databases it is adviable for you to protect table and field names, for example with backticks in MySQL. The Active Record class
108 automatically protects its queries. If you need to manually protect an identifier you can use the following.
</p>
110 <div class=
"example">
111 $this-
>db-
>protect_identifiers('table_name');
115 <div class=
"content">
116 <h1>Escaping SQL Queries
</h1>
118 <p>It is very good practice to escape your data that is being inserted into a database. CarbonPHP provides
119 two methods to help you do this.
</p>
122 <li><b>$this-
>db-
>escape()
</b> - this method determines the data type so that it can escape only string
123 data. It also automatically adds single quotes around the data.
</li>
124 <li><b>$this-
>db-
>escape_str()
</b> - this method escapes data regardless of the type. Most of the time
125 the above method will be used instead of this one.
</li>
129 <div class=
"content">
130 <h1>Query Binding
</h1>
132 <p>Binding enables you to simplify your SQL query syntax by letting CarbonPHP put the queries together for you.
133 Consider the following example.
</p>
135 <div class=
"example">
136 $sql = 'SELECT * FROM `some_table` WHERE `id` = ? AND `status` = ? AND `author` = ?';
<br />
138 $this-
>db-
>query($sql, array(
3, 'inprint', 'Charlie'));
141 <p>The question marks in the query are replaced by the values found in the array passed to the method in the
142 second parameter. Another benefit of using binds are that they automatically escape values producing