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));
Related
I am working on PHPStorm with a codeigniter framework and the MVC architecture.
I need a function in my model that would query the sysuser table in my database and return the uid (User Id) when the username is passed.
sysuser Table structure
+---------------+----------------+-----------------+
| uid | name | username |
+---------------+----------------+-----------------+
| 1 | kim | kcdla |
+---------------+----------------+-----------------+
| 2 | Sam | sammyG |
+---------------+----------------+-----------------+
In my function, if I pass, for example, the username kcdla I need to retrieve the uid as 1 so that I can store this to a variable as $uid and return it to another function that will use it. But currently the result is an array and when I tried to access only the first cell from the array, I get an Exception.
Code in Context
public function getUserUid($username)
{
$this->db->select("*");
$this->db->from('sysuser');
$this->db->where('username',$username);
$query = $this->db->get();
$tableArray = $query->result();
foreach ($tableArray as $row) {
$uid = $row->uid;
}
return $uid;
}
I need to use the $uid value returned from this method within another method as shown below.
public function getUserDetails($username)
{
$uid = $this->userModel->getUserUid($username);
$this->db->select("*");
$this->db->from('userProfile');
$this->db->where('uid',$uid);
$query = $this->db->get();
return $query->result();
}
Any suggestions in this regard will be highly appreciated.
Try this. As you need single row based on username, you have to use row()(returns Object) or row_array() (returns array) instead of result() because result() or result_array() returns result set
public function getUserUid($username)
{
$this->db->select("*");
$this->db->from('sysuser');
$this->db->where('username',$username);
$query = $this->db->get();
$row = $query->row();
return $row->uid;
}
call the function like this
public function getUserDetails($username)
{
$uid = $this->getUserUid($username);
^^^^^^^^^^^^^^^^^^^^^^^^^^
$this->db->select("*");
$this->db->from('userProfile');
$this->db->where('uid',$uid);
$query = $this->db->get();
return $query->result();
}
Return the single row id like this
row(), returns only the first row. It's also an object, and if you need an array, you can use row_array()
public function getUserUid($username)
{
$this->db->select("*");
$this->db->from('sysuser');
$this->db->where('username',$username);
$query = $this->db->get();
$row_values = $query->row_array();
return $row_values['uid'];
}
As per your comment try this
public function getUserDetails($username)
{
$uid = $this->userModel->getUserUid($username);
$this->db->select("*");
$this->db->from('userProfile');
$this->db->join('another_table', 'another_table.uid = userProfile.uid');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$this->db->where('userProfile.uid',$uid);
$query = $this->db->get();
return $query->result();
}
Hi i got a problem with a query with CodeIgniter 3.
I'm trying to get all users from database with limit and start excluding the ID of the user who is getting the list.
That's my model:
public function getUserList($id,$limit,$start){
$this->db->select('*');
$this->db->from('register');
$this->db->where('register.id' != $id);
$this->db->order_by("id");
$this->db->limit($limit,$start);
$result = $this->db->get();
return $result->result_array();
}
And that's the controller:
public function users(){
$this->load->model('User_model');
$id = $_GET['id'];
$limit = $_GET['limit'];
$start = $_GET['start'];
$data = $this->User_model->getUserList($id,$limit,$start);
echo "<pre>"; print_r($data); die;
$this->load->view('api', $data);
}
The problem is this function return me an empty array instead an array of 5 array.
Custom key/value method:
You can include an operator in the first parameter in order to control the comparison:
Change below line.
$this->db->where('register.id !=', $id);
I have function to fetch the products from ci_products table
the code is as follows
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$products = $this->db->get();
return $products;
}
now I want to fetch from where user_id is current user_id
I have tried few things but not working
what I have tried`
function get_product_selections($product_ids = array())
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where_in('product_id', $product_ids);
$this->db->where('user_id',$this->session->userdata('user_id'));
$products = $this->db->get();
return $products;
}
$data = $this->db->get_where('product_ids',$product_ids);
$this->db->where_in('product_ids',$data);
$this->db->where('user_id',$this->session->userdata('user_id'));
$query = $this->db->get();
return $query->result_array();
Try this one :
In your first code:
function get_product_selections($product_ids)
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('product_id', $product_ids);
$products = $this->db->get();
return $products->result_array();
}
what do you mean when you put array() in the parameter? you want to retrieve an array() of products?
function get_product_selections()
{
$this->db->select('*');
$this->db->from('ci_products');
$this->db->where('user_id',$this->session->userdata('user_id')); // the user who is currently login
$products = $this->db->get();
return $products->result_array(); // return the results
}
Perhaps a simple question:
This is my striped code in my model:
public function trainees()
{
$this->db->select('*');
$this->db->from('relations');
$query = $this->db->get();
if ($query->num_rows() >0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
}
My controler:
public function adresslist() {
$this->load->model('Adresslist');
$data['trainees'] = $this->Adresslist->trainees();
$data['numrows'] = ??????
$this->load->view('adresslist', $data);
}
And in my view I load the table data with a foreach
works perfect.
But how do I send the value of num_rows to my controler?
The number of trainees is simply the length of the array returned, you don't need to know num_rows
$data['numrows'] = count( $data['trainees']);
will do the trick
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.