Skip to content

Instantly share code, notes, and snippets.

@todgru
Created December 19, 2012 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todgru/4333328 to your computer and use it in GitHub Desktop.
Save todgru/4333328 to your computer and use it in GitHub Desktop.
CI queries bindings code igniter

http://ellislab.com/codeigniter/user-guide/database/queries.html

"...benefit of using binds is that the values are automatically escaped..."

Of note, Query Bindings looks like the easiest method:

$this->db->query( " SELECT id FROM users WHERE username = ? AND age = ? ",  array ( 'todd', '37' ) );

###Query Bindings

Bindings enable you to simplify your query syntax by letting the system put the queries together for you. Consider the following example:

$sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?"; 
$this->db->query($sql, array(3, 'live', 'Rick'));

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment