CI Active record not work in Google Chrome? - php

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

Related

show error when codeigniter join with multiple database tables

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; }

Undefined member function error at num_row(). in codeigniter framework

Error at num_rows() as undefined member function. For dispalying data in descending order in codeigniter Framework.
I tried this code:
$this->db->select('*');
$this->db->from('albums');
$result = $this->db->order_by('album_id', 'desc');
//$result = $this->db->get('albums');
if($result->num_rows() > 0)
{
return $result;
}
For fetching and displaying rows in descending order i used the above code. But i am getting the following error at num_rows() as undefined function. How do I print it in descending order?
Change you'r code to :
$this->db->select('*');
$this->db->from('albums');
$result = $this->db->order_by('album_id', 'desc');
$result = $this->db->get('albums')->row();
if($result->num_rows() > 0)
{
return $result;
}
You will need to execute get() before getting the count of rows.
So your Code should look like this,
$this->db->select('*');
$this->db->from('albums');
$this->db->order_by('album_id', 'desc');
$result = $this->db->get(); // add this line
if($result->num_rows() > 0)
{
return $result;
}
get() function runs the selection query.
Use this code
$this ->db->select('*');
$this ->db->from('albums');
$this->db->order_by('album_id', 'desc');
$query = $this->db->get();
if($query->num_rows() > 0)
{
return $query->result();
}
If you need help hit me up :)
EDIT
You need
$query->result();
To return it as an object, but you you like you can use
$query->result_array();
To return it as array, don't just return $query as it is :)

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 error with selecting data from table

I cant figure out whats wrong with my model, it said a Fatal Error occured.
Here's my model file:
function getName($no_ktp){
$this->db->select('nama')->from('dt_prbd')->where('no_ktp', $no_ktp);
$qry_getName = $this->db->get();
if ($qry_getName->num_rows() > 0) {
foreach ($qry_getName->result() as $data_getName){
$hasil_qry_getName[] = $data_getName;
}
return $hasil_qry_getName;
}
}
I got this error.
Fatal error: Call to a member function num_rows() on boolean in C:\xampp\[APP_PATH]\M_hrd_apps.php on line 25
I thought the error is in the query, so I changed it into this:
$qry_getName =
$this->db->select('nama')
->from('dt_prbd')
->where('no_ktp', $no_ktp)
->get();
but the error is the same,
Call to a member function num_rows() on boolean
Can anyone help me please?
Tr this
function getName($no_ktp){
$this->db->select('nama')->from('dt_prbd')->where('no_ktp', $no_ktp);
$qry_getName = $this->db->get();
$result = $qry_getName->result();
return $result;
}
}
Try this
In Model
function getName($no_ktp)
{
$query = $this->db->query("SELECT nama FROM dt_prbd WHERE no_ktp= '$no_ktp'");
$result = $query->result_array();
$count = count($result);
if (empty($count))
{
$log = 0;
return $log;
}
else
{
return $result;
}
}
In controller
$result = $this->Model_name->getName();
if($result == 0)
{
echo 'Data Is empty';
}
else
{
$data['name'] = $result;
#rest of your code
}
EDIT 01
To configure your database
go to config/database.php in bottom of page define
Databse name
Mysql Username
Mysql Password
all problem solved, there is no error in code. i try to re-create the db, and then all script turn out like what i expect.
thanks for all help Deep Parekh and Abdulla

Codeigniter with Ignited Datatables

I have finally got Codeigniter to work with ignited datatables. Now i have run into a different problem. Can anyone help or tell me if i could run the below query with the datatables plugin for codeigniter.
At present i'm doing it within the controller which is lame i know (this was only for testing)
Controller
$data['query'] = $this->test_queries->list_partners();
foreach($data['query'] as $k => $company){
$data['query'][$k]->partner_contacts = $this->test_queries->get_partner_contacts($company->id);
}
Queries in the Model
function list_partners(){
$this->db->select("company.id,name,general_email,general_phone,market");
$this->db->from("company");
$this->db->join('markets','markets.id = company.market_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
function get_partner_contacts($id){
$this->db->select('partner_contacts.id,contact_type');
$this->db->from('partner_contacts');
$this->db->where('company_id',$id);
$this->db->join('department','department.id = partner_contacts.contact_type_id');
$query = $this->db->get();
$result = $query->result();
return $result;
}
You can change the queries in the model as below:
function list_partners(){
$this->datatables->select("company.id,name,general_email,general_phone,market");
$this->datatables->from("company");
$this->datatables->join('markets','markets.id = company.market_id');
return $this->datatables->generate();
}
function get_partner_contacts($id){
$this->datatables->select('partner_contacts.id,contact_type');
$this->datatables->from('partner_contacts');
$this->datatables->where('company_id',$id);
$this->datatables->join('department','department.id = partner_contacts.contact_type_id');
return $this->datatables->generate();
}
You can also use method chaining if you are using PHP 5 or above.

Categories