Issue
Following the code igniter docs when I try to execute the following sql query I get this exception ? Would apprecate some help to point out the issue with the query
$query = $this->db->join('price', 'price.itemId = shop.itemId', 'right');
var_dump($query->result_array());
if ($query->num_rows() > 0){
return $query->result();
}
To give some background I have two tables shop and price. price table has this primary key itemId which is a foreign key in the price table. I want to get all records in the pricetable along with an attribute called name which is in the shop table
Solution
When you use the Join statement with the codeigniter query builder you must tell it with what table you want to join with.
So your code would look something like this:
$query = $this->db->join('price', 'price.itemId = shop.itemId', 'right')
->get('shop');
return $query->result();
Also there's no need to check if there's any rows to actually return something, just return the result like I did here, and you'll have returned an empty array. That way your function always return the same thing in this case an array.
It's not really good to return null and array in the same function.
For more information on how the query builder works here's a link to a very extensive documentation.
https://codeigniter.com/userguide3/database/query_builder.html?highlight=query%20builder
Answered By - marcogmonteiro
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.