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;
Related
I use this code to display data according to the user who is logged in, and display their data.
but with this syntax the data that appears does not match the defined data, and also other user data sometimes appears too.
$id=$this->session->userdata('ses_id');
$this->db->SELECT('*');
$this->db->from('doc_priv_std');
$this->db->join('acc',' doc_priv_std.id_acc=acc.id_acc');
$this->db->where('doc_priv_std.id_acc ',$id,('and
type_doc LIKE "%.doc%"
or "%.docx%"
or "%.pdf"
or "%.xls%"
or "%.xlsx%"
or "%.ppt%"
or "%.pptx%"
or "%.zip%"
or "%.rar%"'));
$query = $this->db->get();
return $query->result_array();
please help so that the data that appears as defined.
Your syntax is wrong - you can't do this with your way - you have to organise your Query properly - try the following
$id=$this->session->userdata('ses_id');
$arrExtension = ['.docx', '.pdf', '.xls', '.xlsx', '.ppt', '.pptx', '.zip', '.rar'];
$this->db
->select('*')
->from('doc_priv_std')
->join('acc',' doc_priv_std.id_acc=acc.id_acc')
->where('acc.id_acc', $id)
->group_start();
foreach($arrExtension AS $strExtension)
{
$this->db->or_like('type_doc', $strExtension);
}
$query = $this->db->group_end()->get();
return $query->result_array();
Use var_dump($this->db->last_query()); before your return to see the db query you are actually running to help with debugging, but it could be that you where statement is wrong? Try this instead:
$where = 'WHERE type_doc LIKE "%.doc%"
or "%.docx%"
or "%.pdf"
or "%.xls%"
or "%.xlsx%"
or "%.ppt%"
or "%.pptx%"
or "%.zip%"
or "%.rar%"';
$this->db->select('*')
->join('acc',' doc_priv_std.id_acc=acc.id_acc')
->where('doc_priv_std.id_acc', $id)
->where($where);
$query = $this->db->get();
return $query->num_rows() > 0 ? $query->result_array() : FALSE;
I am trying to join two tables and return an array in my Model method in CodeIgniter with php. I've gone through a few previously posted similar questions on stackoverflow and modified my code accordingly. But they don't seem to work. Hence would love to know what's wrong with the following.
I'm using the following method but am currently getting exceptions. Would appreciate suggestions in this regard.
Model Method
public function getUserDetails($username)
{
$uid = $this->getUserUid($username);
$this->db->select('s.uid, s.name,s.phone, s.studentId, s.type, sp.profiledesc, sp.age');
$this->db->from('sysuser as s');
$this->db->join('studentprofile as sp', 's.uid = sp.uid', 'left');
$this->db->where(array('s.uid' => $uid));
$query = $this->db->get();
return $query->result();
}
Controller
$data1['details'] = $this->userModel->getUserDetails($username);
$this->load->view('studentDashboard/viewProfile',$data1);
View
...
<h2>
<?php foreach($details as $detail){?>
<?php echo $detail->s.name;?>
<?php }?>
</h2>
...
In the view, I've also tried just echoing $detail->name but this doesn't work either.
At first, use print_r($details) for checking your data. If it's returning anything or not.
Then echo your value like this $detail['name']
Fixed Code:
public function getUserDetails($username)
{
$uid = $this->getUserUid($username);
$this->db->select("*");
$this->db->from('sysuser');
$this->db->join('studentprofile', 'studentprofile.uid = sysuser.uid');
$this->db->where('sysuser.uid',$uid);
$query = $this->db->get();
return $query->result();
}
Look, i´m not sure that i understood your code, what means this line
$uid = $this->getUserUid($username);
You´re calling a method and sending the name to retrieve the userid, right?
I´ll write that method like you should have it:
public function getUserid($user){
$this->where->id($user);
return $this->get('whatever table')->row();
//i think you forgot this ->row()
}
then
public function getUserDetails($username)
{
$uid = $this->getUserUid($username);
//here already you should bring with ->row() already
//you can use this var_dump here to confirm too
//var_dump($uid);
//exit;
$this->db->select('s.uid, s.name,s.phone, s.studentId, s.type, sp.profiledesc, sp.age');
//line´s right
//the from method is disposable, so i put it into the get but here it´s right too
$this->db->join('studentprofile as sp', 's.uid = sp.uid', 'left'); //ok
$this->db->where($uid); //this line is the wronger, i´ve made the fix
//as this is an where, you´d bring only one value with ->row() from that other method
$query = $this->db->get('sysuser as s');
//the 'from' i putted here, just to write a line less
return $query->result();
when you need to test what you´re returng do a var_dump here
//commenting the return above
//$test = $query->result();
//var_dump($test);
}
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();
I want to perform this query wth active records :
SELECT * FROM tablename WHERE status = 'A' AND name LIKE 'test'
And i want it to return an array because i want to encode it to json later, so i need to use result_array();
So i tried something like this :
$query = $this->db->select('*')->from('tablename')->where('status', 'A');
$query->like('name', 'test')->get()->result_array();
return $query;
But i got this message when i tried to encode it to json :
type is unsupported, encoded as null
What should i do? Thanks for your help.
Try this:
$data = array();
$rs = $this->db->where('status', 'A')->like('name', 'test')->get('tablename');
if($rs->num_rows()> 0){
$data = $rs->result_array();
}
return $data;
this structure of code doesn't give any error :-
$this->db->where('status', 'A');
$this->db->like('name', 'test')
$query=$this->db->get(tablename);
$data=$query->result();
$json_data=json_encode($data);
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;