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

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 :)

Related

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

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

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.

Undefined index when returning database results in Codeigniter

I am trying to transfer data over from a Wordpress database into a Codeigniter project. I have a model method that returns a single value, but I keep getting the following:
Message: Undefined index: meta_value
Filename: models/transfer_model.php
Here's the model:
public function get_meta($post_id, $key)
{
$this->db->select('meta_value');
$this->db->from('wp_postmeta');
$this->db->where('post_id', $post_id);
$this->db->where('meta_key', $key);
$this->db->limit(1);
$query = $this->db->get();
$row = $query->row_array();
return $row['meta_value'];
}
If I print_r($row);, I get the following:
Array
(
[meta_value] => The Data
)
How can I stop it from returning this error?
where there record fetched you are getting return when there is no record fetched with query then there will be nothing means your row is empty to avoid this use key_exists or array_key_exists have look on php doc
array_key_exists
public function get_meta($post_id, $key)
{
$this->db->select('meta_value');
$this->db->from('wp_postmeta');
$this->db->where('post_id', $post_id);
$this->db->where('meta_key', $key);
$this->db->limit(1);
$query = $this->db->get();
$row = $query->row_array();
$return = '';
if(key_exists('meta_value',$row))
$return = $row['meta_value'];
return $return;
}
What if you use an integer index?
$row = $query->row_array();
return $row[0];

Categories