codeigniter get_where() function - php

I am using active record on an old codeigniter installation and I am running into problems executing multiple queries using the get_where function. for instance the following code
$this->db->get_Where('activation', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
$this->db->get_Where('users', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
the first query generates
SELECT * FROM (`activation`) WHERE `email` = 'test#yahoo.com'
the second one throws me for a loop and generates
SELECT * FROM (`activation`, `users`)
WHERE `email` = 'test#yahoo.com' AND `email` = 'test#yahoo.com'
Am i supposed to be clearing something?

$this->db->get_Where('activation', array('email'=>'test#yahoo.com'))->row();

Use this:
$this->db->select('*')->where('email','test#yahoo.com')->get('activation')->result()

You need to retrieve the results from your query with $this->db->query->result();
$this->db->get_Where('activation', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
$result_1 = $this->db->query->result();
$this->db->get_Where('users', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
$result_2 = $this->db->query->result();
or if you want to return an array use result_array(); instead of result();

If you use multiple get_where statements in same function then it will be concatenated.
So Better use a condition for those two conditions to execute a single query at a time.
For example,
if(//condition 1)
{
$query=$this->db->get_Where('activation', array('email'=>'test#yahoo.com'));
}
else{
$query=$this->db->get_Where('users', array('email'=>'test#yahoo.com'));
}
//finally get the active record.
echo $this->db->last_query();
Otherwise you wont achieve.
Please mention the purpose to get two table records.

$this->db->get_Where('activation', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
$result_1 = $this->db->get()->result_array();
this will execute the first query
and then
$this->db->get_Where('users', array('email'=>'test#yahoo.com'));
echo $this->db->last_query();
$result_2 = $this->db->get()->result();
this will execute the second query then u can do below code to print the two results
echo "<pre>";print_r($result_1);print_r($result_2);
echo "</pre>";

You need to use it like this:
$this->db->get_where('users', array('email'=>'test#yahoo.com'))->result();
Well apart from using get_where you can simply use it like this:
$this->db->select('*');
$this->db->from('activation');
$this->db->where('email','test#yahoo.com');
or
$this->db->where(array('email'=>'test#yahoo.com'));

$this->db->get_where($table, $where, $limit, $offset)
$where = array('FIELD_NAME'=>value)
result_array();

Related

Can't get result from $this->db->last_query(); codeigniter

Quite a simple thing to ask and must be discussed many times, but I still not able to get the result of $this->db->last_query();.
$this->db->select('count(*) as totalverified,res_sales.upduser, employee.name');
$this->db->from('res_sales');
$this->db->join('employee','employee.user_id = res_sales.upduser');
$this->db->where('date>=', $fromdate);
$this->db->where('date<=', $todate);
$this->db->where('verificationnumber<>', '');
$this->db->where('verificationnumber<>', NULL);
$this->db->group_by('res_sales.upduser');
$this->db->group_by('employee.name');
$q = $this->db->get();
$str = $this->db->last_query();
print_r($str);
if ($q->num_rows() > 0)
{
return $q->row();
}
return FALSE;
Above is the code in my model function. I am not able to get the result as expected to want to see what the query is being run at backend.
Thanks.
Danish
I figured out the problem. I have to write a statement just above my query i.e:
$this->db->save_queries = TRUE;
After this write your query and than write $this->db->last_query(); it is showing the last query now.
Sample:
$this->db->save_queries = TRUE;
$str = $this->db->last_query();
echo $str;
Cheers.
Create a helper function and use it anywhere in your project:
function lq(){ echo $this->db->last_query(); die; }
and call to controller and model like:
lq();
I want to ask why are printing query like array simply use
$str = $this->db->last_query();
echo $str;

using aggregate function in codeigniter query

I am trying to write this below query in codeigniter format and getting some problem with the aggregate function:
$stmt = "select sum(subscription_amt) as samt, bill_month,
sum(loan_refund_amt*no_of_loan_installment+error_amt) as lamt
from pf_bill_det
where trim(pf_number)='$pfno'
and fin_year='$fyear'
and aproved='Y' group by bill_month";
$query = $this->db->query($stmt);
This query ending up with an error loan_refund_amt*no_of_loan_installment+error_amt is not a column. Please help me how to write this query using codeigniter query format.
why don't you try this
$this->db->select("COUNT(*) AS MyCount");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
OR
$this->db->select("SUM(field_name) AS MySum");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
OR
$this->db->select("SUM(field_name) AS MySum, username, password");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
BUT
In simple query function you can use
$query = $this->db->query("SELECT COUNT(field_name) AS total_names, fname");
$query->result(); \\ Returns an array of objects
$query->result_array(); \\ Returns result as a pure array
$query->row(); \\ Returns a single result and first row
Try this,
$query=$this->db->query("select sum(subscription_amt) as samt,bill_month,sum(`loan_refund_amt`*`no_of_loan_installment`+`error_amt`) as lamt from pf_bill_det where trim(pf_number)='$pfno' and fin_year='$fyear' and aproved='Y' group by bill_month");

Query resulting empty in Codeigniter

I have piece of PHP code written in Codeigniter framework that returns nothing (an empty set).
Have a look at it and tell me what is wrong with it.
function sbsn($serial){
$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('asset_types,asset_brands,assets');
$this->db->where('assets.type_code','asset_types.code');
$this->db->where('assets.brand_code','asset_brands.code');
$this->db->where('serial_no',$serial);
$result = $this->db->get();
return $result;
}
hi with db get you are only getting result set not record to get results from result you have to the following thing
$this->db->get()->result(); // will return an array of objects
$this->db->get()->result_array(); //will return result in pure array
$this->db->get()->row() // just single row as object
$this->db->get()->row_array() // just single row as array
you can use $result to perform the same above things
$result->result();
$result->row();
for more information read generating result user guide
change:
$result = $this->db->get();
to
$result = $this->db->get()->row_array(); //to get single row
//OR
$result = $this->db->get()->result_array(); //to get multiple rows
OR try using JOIN, like
$this->db->select('asset_types.name as type_name,asset_brands.name as brand_name');
$this->db->from('assets');
$this->db->join('asset_types', 'asset_types.code = assets.type_code', 'left');
$this->db->join('asset_brands', 'asset_brands.code = assets.brand_code', 'left');
$this->db->where('assets.serial_no',$serial);
$result = $this->db->get()->result_array();
return $result;
The problem can be easily solved by joining the tables in the following way.
$this->db->select('at.name as type_name,ab.name as brand_name');
$this->db->from('asset_types as at,asset_brands as ab');
$this->db->join('assets as a', 'a.type_code = at.code and a.brand_code as ab.code');
$this->db->where('a.serial_no',$serial);
$result = $this->db->get()->result_array();
return $result;

unable to count the result with a condition using codeigniter query

I am trying to count rows of bookdetails table where display_id is as argument. Say i passed $id='3'. But i am not getting the output.
I think the code which i am trying is wrong. Please help me to write this query correctly
//--- Counting the rows of bookdetails of table where display_id is as argument-------------------------------------------------------------
public function record_count_for_secondtopBooks($id) {
$this->load->database();
return $this->db->count_all("bookdetails",array('display_id'=>$id));
}
count_all returns the number of rows in a particular
echo $this->db->count_all('my_table');
Try this
$this->db->where('display_id', $id);
$this->db->from('bookdetails"');
$this->db->count_all_results();
count_all accepts only one argument and that is table name. So you will get count of all records in that table. as written in manual:
Permits you to determine the number of rows in a particular table.
Submit the table name in the first parameter. Example:
$this->db->where('display_id',$id);
$result = $this->db->count_all("bookdetails");
or Chain em'
$result = $this->db->where('display_id',$id)->count_all("bookdetails");
check:
echo'<pre>';
print_r($result);
echo'</pre>';
Just try this,
$this->db->where('display_id', $id);
$query = $this->db->count_all('bookdetails');
return $query;
please try below code
public function record_count_for_secondtopBooks($id) {
$this->db->where('display_id',$id);
$q = $this->db->get('bookdetails');
return $q->num_rows();
}
try this
public function record_count_with_where($table_name,$column_name,$type)
{
$this->db->select($column_name);
$this->db->where($column_name,$type);
$q=$this->db->get($table_name);
$count=$q->result();
return count($count);
}

Codeigniter Query doesn't return result value

I got the following snippet to get some userrights out of the db:
$tmp = "SELECT REPLACE(group_concat(CAST(".$role." AS CHAR)),',','') AS rights FROM functionrights ORDER BY id ASC";
$query = $this->CI->db->query($tmp);
if($query->num_rows()>0){
$row = $query->row();
return $row->rights;
This returns nothing. If I execute the statement direct everything is ok?! What is wrong here?
Change $this->CI->db->query to $this->db->query
The ->CI is not necessary. Also, you have no } tag (but that could be just a copy/paste thing).
Besides that: ->row() returns one result, while ->result() returns all results.

Categories