Welcome Guest, Not a member yet? Register   Sign In
MYSQL / Query Builder Class help
#10

The documentation about models starts with this remark:
Models are optionally available for those who want to use a more traditional MVC approach.

MVC stands for model - view - controller.
In CodeIgniter, models are optional, although they often bring a lot of benefits, especially if you need to perform the same database operations from different controllers.

The $this->db->query(...) function is OK also. I prefer the Query Builder, especially when I have to use where() clauses that depend on different situations.
Example in plain SQL:
PHP Code:
$sql 'SELECT * FROM posts';
if (
$condition1 == TRUE) {
 
 $sql .= ' WHERE date >= "' $date1 '"';
}
if (
$condition2 == TRUE) {
 
 if ($condition1 == TRUE) {
 
   $sql .= ' AND status="A"';
 
 }
 
 else {
 
   $sql .= ' WHERE status="A"';
 
 }
}
$query $this->db->query($sql); 

The same with Query Builder:
PHP Code:
if ($condition1$this->db->where('date >=' $date1);
if (
$condition2$this->db->where('status''A');
$query $this->db->get('posts'); 

Do you notice the difference? Query Builder is definitely better here, way shorter and easier to understand.

The way you let CI return the result(s) (as objects or as arrays), is totally up to you. In most cases, I use objects, because it's easier. Compare $post->title to $post['title']. There is no good or bad here. Use the method that you find most convenient.

Remember this:
PHP Code:
$query->row();  //returns only one record, as an object
$query->row_array();  //returns only one record, as an array
$query->result();  //returns an array of (multiple) records; each record is an object
$query->result_array();  //returns an array of (multiple) records; each record is an array 
Reply


Messages In This Thread
MYSQL / Query Builder Class help - by neoraj3 - 08-05-2017, 12:23 PM
RE: MYSQL / Query Builder Class help - by neoraj3 - 08-06-2017, 04:46 AM
RE: MYSQL / Query Builder Class help - by neoraj3 - 08-06-2017, 11:25 AM
RE: MYSQL / Query Builder Class help - by neoraj3 - 08-06-2017, 04:47 AM
RE: MYSQL / Query Builder Class help - by neoraj3 - 08-06-2017, 11:22 AM
RE: MYSQL / Query Builder Class help - by neoraj3 - 08-13-2017, 06:46 AM
RE: MYSQL / Query Builder Class help - by Wouter60 - 08-13-2017, 10:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB