Codeigniter Running Queries reference – This database reference is used for submit a query and use the query function. We can also use this reference to identify regular, simplified and escaping query. Here in this tutorial, we are going to explain how to use running queries reference.
Codeigniter Running Queries Reference | Example.
Let us understand how running queries reference works in codeigniter with examples.
Functions:-
There are following functions available in running queries reference. Now we will explain one by one.
- 1. Regular Queries.
- 2. Simplified Queries.
- 3. Working with Database prefixes manually.
- 4. Protecting identifiers.
- 5. Escaping Queries.
- 6. Query Bindings.
- 7. Handling Errors.
1. Regular Queries.
Here is simple demo of regular queries.
Syntax of regular query reference.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class running_query_controller extends CI_Controller { public function RegularQuery() { $this-?>db->query('select * from blog'); } } ?> |
This query function return database result.
2. Simplified Queries.
Here is simple demo of simplified queries.
Syntax of simplified Query reference.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class running_query_controller extends CI_Controller { public function query() { if ($this-?>db->simple_query('select * from blog')) { echo "Success!"; } else { echo "Query failed!"; } } } ?> |
The simple_query method is a simplified version of the $this->db->query() method.
Output will be like this:-
3. Working with Database prefixes manually.
Here is simple demo of working with database prefixes manually.
Syntax of working with database prefixes manually.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class running_query_controller extends CI_Controller { public function data() { $this-?>db->set_dbprefix('Database table name is '); echo $this->db->dbprefix('blog'); } } ?> |
Output will be like this:-
4. Protecting identifiers.
Here is simple demo of protecting identifiers.
Syntax of protecting identifiers.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class running_query_controller extends CI_Controller { public function protect() { echo $this-?>db->protect_identifiers('blog'); } } ?> |
This function is used to protect table and field name in database.
Output will be like this:-
5. Escaping Queries.
Here is simple demo of escaping queries.
Syntax of escaping queries.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class running_query_controller extends CI_Controller { public function escape() { $sql = "INSERT INTO table_name (title) VALUES('".$this-?>db->escape_str($title)."')"; } } ?> |
It is a best security practice to escape your data before submitting it into your database.
6. Query Bindings.
Here is simple demo of query bindings.
Syntax of query bindings.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class running_query_controller extends CI_Controller { public function binding() { $sql = "SELECT * FROM blog WHERE id = ? AND title = ? AND name = ?"; $result = $this-?>db->query($sql, array(3, 'Job', 'Ram')); print_r($result); } } ?> |
The question marks in the query are automatically replaced with the value in the array in second paerameter in the query function.
7. Handling Errors.
Here is simple demo of handling errors.
Syntax of handling errors.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class running_query_controller extends CI_Controller { public function ErrorHandling() { if ( ! $this-?>db->simple_query('SELECT `title` FROM `blog`')) { $error = $this->db->error(); echo $error; } else { echo "success"; } } } ?> |
Output will be like this:-