show error when codeigniter join with multiple database tables - php

show Call to a member function num_rows() on boolean when I going to join with two different database on production. on localhost working fine
my code is below :
$this->db->select('livedb.ADMIN_TEAM.id');
$this->db->from('livedb.ADMIN_TEAM');
$this->db->join("olap.SL_DEMO_ACTIVITY_COUNT","(olap.SL_DEMO_ACTIVITY_COUNT.admin_id = livedb.ADMIN_TEAM.id) AND (olap.SL_DEMO_ACTIVITY_COUNT.assign_date='".$preData."')" ,"LEFT");
$this->db->join("olap.SL_DEMO_ACTIVITY_COUNT","(olap.SL_DEMO_ACTIVITY_COUNT.admin_id = newforce_livedb.ADMIN_TEAM.id)" ,"LEFT");
$this->db->where('livedb.ADMIN_TEAM.team_status','1');
$this->db->where('livedb.ADMIN_TEAM.team_type','sales');
$this->db->where('livedb.ADMIN_TEAM.id !=','129');
$this->db->where('livedb.ADMIN_TEAM.sale_type','CRM');
$this->db->limit('1');
$this->db->order_by('livedb.ADMIN_TEAM.id','ASC');
$query = $this->db->get();
print_r($this->db->last_query());
if( $query->num_rows() > 0 ){
return $query->result();
}
else { return FALSE; }

Related

Fatal error: Can't use function return value in write context in Codeigniter

This is the function I am using in my model
public function user_birthday() {
$this->db->select('birth_day')
->from('informations')
->where(DATE_FORMAT(FROM_UNIXTIME('birth_day'), '%m-%d') = DATE_FORMAT(NOW(), '%m-%d'));
$q = $this->db->get();
return $q->result();
}
function in controller like $this->data['user'] = $this->users_m->user_birthday(); that way
and code in the view is
if (!empty($user)):
echo $user;
else:
echo "No dob found";
endif;
This is one of those circumstances where Query Builder is more trouble that it is worth. Try this
$q= $this->db->query("SELECT birth_day FROM informations WHERE DATE_FORMAT(birth_day, '%m-%d') = DATE_FORMAT(NOW(), '%m-%d')");
//return empty array if not records found
return $q->num_rows() > 0 ? $q->result() : [];
If you really, really, really want to use Query Builder then this
$q = $this->db
->select('birth_day')
->where("DATE_FORMAT(birth_day, '%m-%d')=", "DATE_FORMAT(NOW(), '%m-%d')", FALSE)
->get('informations');
return $q->num_rows() > 0 ? $q->result() : [];

num_rows() function is not working for MSSQL in codeigniter

I m using codeigniter with MSSQL db. to get a number of records from a query I m using $query->num_rows(). but its not working. If I use MYSQL DB then its working fine. My code is -
function getCountry(){
$this->db->distinct();
$this->db->select('CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE as CNTRY_CODE, CM_COUNTRY.CC_CNTRY_NAME as CNTRY_NAME');
$this->db->from('CM_CHANNEL_TELCAS_DETAIL');
$this->db->join('CM_COUNTRY','CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE = CM_COUNTRY.CC_CNTRY_CODE','inner');
$query = $this->db->get();
if($query -> num_rows() > 0){
return $query->result();
}else{
return false;
}
}
If I write above code like this then It gives the results -
function getCountry(){
$this->db->distinct();
$this->db->select('CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE as CNTRY_CODE, CM_COUNTRY.CC_CNTRY_NAME as CNTRY_NAME');
$this->db->from('CM_CHANNEL_TELCAS_DETAIL');
$this->db->join('CM_COUNTRY','CM_CHANNEL_TELCAS_DETAIL.CTD_CNTRY_CODE = CM_COUNTRY.CC_CNTRY_CODE','inner');
$query = $this->db->get();
return $query->result();
}
I tried to echo $query->num_rows() value. but its not giving any value.
Please help me to resolve this issue.
Thanks in Advance.
Try with other method $this->db->count_all_results()
if($this->db->count_all_results() >0)
{
return $query->result();
}
else
{
return false;
}
try this
echo $query->affected_rows();
hope help youu

Codeigniter not getting data though available in mysql database

I have a model which fetch the data from database is below
public function counselor() {
$inst_id = $this->session->userdata('user_id');
$submission_key=$this->session->userdata('submission_key');
$query = $this->db->query("SELECT * FROM counselor where USER_ID = $inst_id AND submission_key= $submission_key");
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}
}
I have tested the $inst_id and $submission_key by printing it and its set.
$inst_id=2 and $submission_key=2016-8 .BUT though I have one record in database with those two field set its not returning the data. What is the case. I have tried with codeigniter get() and where() method too. Still not giving me the result.
Just write your query using active record function. It will help you in escaping string
$this->db->select('*',FALSE);
$this->db->where('USER_ID',$inst_id);
$this->db->where('submission_key',$submission_key);
$query=$this->db->get('counselor');
$data = $query->num_rows();
if ($data > 0) {
return $data;
} else {
return false;
}

CI Active record not work in Google Chrome?

I write this query in CI.
$this->db->select("initiation.*, project.projectname,subproject.subprojectname, concat(employee.firstname,' ',employee.lastname) as 'name'")
->from('initiation')
->join('project','initiation.projectid=project.id')
->join('employee','initiation.leaderid = employee.id')
->join('subproject','initiation.subprojectid=subproject.id','left');
$query = $this->db->get();
return $query->result();
It works fine in Firefox, but get this error message in Chrome.
Fatal error: Call to a member function result() on a non-object
Any idea please?
Try with result_array(); Make sure you have autoloaded database library
$this->db->select("initiation.*, project.projectname,subproject.subprojectname, concat(employee.firstname,' ',employee.lastname) as 'name'")
->from('initiation')
->join('project','initiation.projectid=project.id')
->join('employee','initiation.leaderid = employee.id')
->join('subproject','initiation.subprojectid=subproject.id','left');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
$this->db->select("initiation.*, project.projectname,subproject.subprojectname, concat(employee.firstname,' ',employee.lastname) as 'name'")
->from('initiation')
->join('project','initiation.projectid=project.id')
->join('employee','initiation.leaderid = employee.id')
->join('subproject','initiation.subprojectid=subproject.id','left');
$query = $this->db->get();
$result = $query->result_array();//changed
return $result;//changed

Counting the number of results returned by a database query in Codeigniter

I am not having much luck detecting when a database query in Codeigniter returns zero results. I have had a good read of the notes on the PHP count function but am none the wiser!
I call the query/view as follows from the controller:
$data['result'] = $this->search_model->do_search(set_value('name'));
$data['title'] = "Search results";
$this->load->view('search_view',$data);
The view generates a results table for me OK, but when I try and trap an empty result, the count always returns 1:
I have tried if count(array($result)) and just if count($result)
So what's a good way to get the count? I'm using Fedora 13 with PHP 5.3.3 on my dev laptop.
Have a look at $query->num_rows (<- clickable).
The best thing to do in your model is the following:
$query = $this->db->something()....
...
...
if ( $query->num_rows() > 0 )
{
return $query->result();
}
else
{
return FALSE;
}
Then in your controller or view you would do the following:
if ( !empty($my_db_result) )
{
......
}
This process enables you to respond on the result based on the result type. If the rows could be retrieved this will return an array of which the items can be counted by PHP's count() function. Since the second block checks if the result is empty (note that "FALSE" is treated as being empty) you won't bump into any issues (e.g. when using a foreach loop) and you can specify what to do in case there were no results.
Try if(isset($result) && count($result)) on your view file then inside the if statement you can write the code you want to executed when the inserts in your db are more than 0...good luck!
If you put count($result) in if statement then when it succeds, it returns only 1.
You can try
$query->num_rows() in a different way.
for example i count to show admins connected ans users connected
Database
in users i add table : [status] [int] [1]
in users also i have : [role] [varchar] [255]
Update statut to 1 (onligne) in login validation()
if($this->model_users->can_log_in($email,$pass)){
$update = array('status' => 1);
$this->model_users->update_onligne($email,$update);
redirect('main/members');
and the model :
public function update_onligne($email,$update){
$this->db->where('email',$email);
$this->db->update('users',$update);
return true;
}
Update status to offline in logout()
Logout controller :
public function logout(){
$id = $this->session->userdata('id');
$update = array('status' =>0);
$this->model_users->logout($id,$update);
$this->session->sess_destroy();
redirect('main/login');
}
Logout model :
public function logout($id,$update){
$this->db->where('id',$id);
$this->db->update('users', $update);
return;
}
Count Onligne :
The Controller :
$data['admin_onligne'] = $this->model_users->count_onligne_admin();
$data['user_onligne'] = $this->model_users->count_onligne_users();
$this->load->view('template/page_left',$data);
The Model :
public function count_onligne_admin(){
$query = $this->db->query('SELECT COUNT(status) AS enligneadmin FROM users WHERE status=1 AND role="admin"')->row_object();
return $query->enligneadmin;
}
public function count_onligne_users(){
$query = $this->db->query('SELECT COUNT(status) AS enligneuser FROM users WHERE status=1 AND role="etudiant"')->row_object();
return $query->enligneuser;
}
The Viewer
<span><?php echo $user_onligne ;?> User en ligne</span>
<span><?php echo $admin_onligne ;?> Admin en ligne</span>
i'm dowing this and it workd for me
Controller
$id = $this->session->userdata('id');
if($this->model_users->if_user_dont_have_email($id)){
....
}
Model Users
public function if_user_dont_have_email($id){
$query = $this->db->query("SELECT email FROM users WHERE id='$id'");
if ($query->num_rows() > 0) {
$row = $query->row_array();
if(empty($row['email'])){
return true;
}else{
return false;
}
}
}

Categories