unable to execute codeigniter query - php

i am trying to execute query like: select*from bookdetails where quantity>0 ORDER BY created_date DESC
but i am getting error like Call to a member function num_rows() on a non-object in...
Please help me to solve this issue.
public function get_all_book_list_ByCreatedDateDSC($limit, $start,$sortsesval)
{
$this->load->database();
$this->db->limit($limit, $start);
$query=$this->db->join('coverdetails', 'coverdetails.cover_id = bookdetails.cover_id')->order_by('created_date', 'DESC')->get_where('bookdetails',array('quantity >', '0'));
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}

Try This:
$this->db->select('*');
$this->db->where('quantity >', $id);
$this->db->join('coverdetails', 'coverdetails.cover_id = bookdetails.cover_id');
$this->db->order_by('bookdetails.created_date', 'desc');
$this->db->limit($limit, $start);
$query = $this->db->get('bookdetails');
if($query -> num_rows() == 1)
return $query->result_array();
else
return false;

Your error message is telling you that $query is not an object. You cant call a member function on a non-object!
Your "join" method is not returning an object.
Try putting print_r($query) or var_dump($query) after you set $query, to find out exactly WHAT it is.

Try your function like this
public function get_all_book_list_ByCreatedDateDSC($limit, $start,$sortsesval)
{
$this->load->database();
return $this->db
->join('coverdetails', 'coverdetails.cover_id = bookdetails.cover_id')
->order_by('created_date', 'DESC')
->limit($limit, $start)
->get_where('bookdetails',array('quantity >', '0'))
->result_array();
}
When your query returns null(found no record) you wont be able to access the object because in $query it is null. You need to return the data in array format so use result_array. For detail see here; Using this you dont need to loop through object. It will give you ready-made array.
In the controller you can check if the result is null or array.

Related

how to get json value on condition from database in codeigniter

public function cmplFirstRdDetails($userMobileNo) {
$this->db->from('records');
$this->db->like('player1', '{"PhoneNumber":"$userMobileNo"}');
$query = $this->db->get();
return $query->result();
}
This code is not working for me.

codeigniter model function with and / or query

I have this function in model
function select($table,$match,$or){
$this->db->select('*');
$this->db->from($table);
$this->db->where($match)->or_where($or);
$query = $this->db->get();
$query_result = $query->result();
return $query_result;
}
and in controller
$post = array("class"=>"XI","section"=>"A","stream"=>"Medical");
$data = $this->main_model->select("class_subjects",array("class"=>$post['class'],"section"=>"all","stream"=>$post['stream']),$post);
print_r($data);
I am not getting any error but this is printing whole data of table. how can i match class='XI' and stream='Medical' and (section='A' or section='all')
Try this
function select($table,$match,$or)
{
$query = $this->db
->select("*")
->from($table)
->group_start()
->where($match)
->group_end()
->group_start()
->or_where($or)
->group_end()
->get();
$strSql = $this->db->last_query();
return $query->result();
}
if that won't work - print out the $strSql Variable and post the Query here.

Codeigniter JOIN multiple tables

I'm having a little trouble in retrieving data in multiple tables using codeigniter.
This is the code i'm using to retrieve data in my model which is working well.
function retrieve_experience($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
function retrieve_education($alumni_id)
{
$this->db->select('*');
$this->db->from('education');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
Now i tried using a simplified code but fails to display the data. here is the code in my model
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->join('education','education.alumni_id=experience.alumni_id');
$this->db->where('experience.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}
In my controller, i used this code to retrieving data in my model
function display()
{
$alumni_id = $this->session->userdata('alumni_id');
$data['all_data'] = $this->Alumni_model->retrieve_all_data($alumni_id);
$data['main_content'] = 'alumni_home';
$this->load->view('includes/template', $data);
}
and for the display i used this code
foreach($all_data as $results)
{
/** data from experience table **/
$results['company_name'];
$results['company_address'];
/** data from education table **/
$results['school_name'];
$results['field_of_study'];
}
I cant display anything at all. Please help
Try the below code,
I believe you'll want something like this:
function retrieve_all_data($alumni_id)
{
$this->db->select("e.*,edu.*");
$this->db->from("experience e");
$this->db->join("education edu", "edu.alumni_id = e.alumni_id",'left');
$this->db->where('e.alumni_id',$alumni_id);
$this->db->group_by('e.exp_id');
$query = $this->db->get();
return $query->result_array();
}
Hope below mentioned function should return data that you expected.
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience ex');
$this->db->join('education ed','ed.alumni_id=ex.alumni_id');
$this->db->where('ex.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}

unable to get the results using where and join condition in codeigniter

i am trying to get the results like select * from bookdetails where display_id = $id with few foreign key join condition
I have written the following query but it is showing error like:
Fatal error: Call to a member function num_rows() on a non-object in C:\xampp\htdocs\project at line432` i.e *if ($query->num_rows() > 0)...
Model.php
public function get_all_book_list_atHomeTop($id, $limit, $start)
{
$this->load->database();
$this->db->limit($limit, $start);
$this->db->get_where('bookdetails', array('display_id' => $id));
//-------join condition ------------------
//------------Ends here Join condition
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}
You are missing table name in get() function:
$query = $this->db->get('bookdetails');
Or you can simply replace it with get_where() statement you have at the beginning:
$query = $this->db->get_where('bookdetails',array('display_id'=>$id));

Codeigniter - Calling a model method within same model is buggy

I am trying to call a model method within same model and its not working as intended. Here is my class with two methods which do not work
class mymodel extends CI_Model{
public function __construct(){
parent::__construct();
$this->tablea = 'tablea';
$this->tableb = 'tableb';
}
public function saveData($data){
$dataCopy['revisionkey'] = $this->getRevisionKey($data['id']);
//check and condition revision key to be int with +1 from last one
$this->db->insert($this->tableb, $dataCopy);
$this->db->where('id', $id);
return $this->db->update($this->user_table, $data) ? true : false;
}
public function getRevisionKey($id){
$this->db->select($this->revision_tablea.'.revisions_number as revisions_number')
->from($this->revision_tablea)
->where($this->revision_tablea.'.id', $id)
->order_by($this->revision_table.'.revisions_number', 'asc')
->limit(1);
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->row_array();
}else{
return 0;
}
}
}
Now method getRevisionKey() should produce a query like following
SELECT `tableb`.`revisions_number` as revisions_number FROM (`tableb`) WHERE `tableb`.`id` = '26' ORDER BY `tableb`.`revisions_number` asc LIMIT 1
but it produces a query like following
SELECT `tableb`.`revisions_number` as revisions_number FROM (`tableb`) WHERE `id` = '26' AND `tableb`.`id` = '26' ORDER BY `tableb`.`revisions_number` asc LIMIT 1
This of course is due to same method being called within the model, this method works fine if used outside of model. Any solution to this problem?
EDIT
Rewriting the getRevisionKey() fixes this. Here is the new version
public function getRevisionKey($id){
$sqlQuery = $this->db->select($this->revision_tablea.'.revisions_number as revisions_number')
->from($this->revision_tablea)
->order_by($this->revision_table.'.revisions_number', 'asc')
->limit(1);
$query = $sqlQuery->where($this->revision_tablea.'.id', $id)->get();
if ($query->num_rows() > 0){
return $query->row_array();
}else{
return 0;
}
}
Here is a simple hack that will give you exactly where you are making a mistake.
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
And save it. Before running the function i mean calling
$this->db->get() // Use $this->db->from() instead
use
$query = $this->db->_compile_select()
and echo $query;
These two functions also help in subquery in codeigniter active record.
How can I rewrite this SQL into CodeIgniter's Active Records?

Categories