There is no result even I have data in the database:
$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('roleID',"4");
$query = $this->db->get();
return $query->result();
In joining you have to define the table name with where condition.Like this...
$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('table_name.roleID',4);//table_name is name of table having column roleID
$query = $this->db->get();
return $query->result();
$this->db->select('*');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('roleID',"4");
$query = $this->db->get();
return $query->result();
your query work with one table data when you join another table you can must define $this->db->where('user.roleID',4) like this.
you can write this query like this for when you get the join table data
$this->db->select('user.*,userprofile.youdesirename,classroom.class name');
$this->db->from('user');
$this->db->join('userprofile', 'user.userID = userprofile.userID');
$this->db->join('classroom', 'user.classroomID = classroom.classroomID');
$this->db->where('user.roleID',4)
$query = $this->db->get();
return $query->result();
Related
here is my code of model
$this->db->select('*');
$this->db->from('vf_training_district', 'vf_training_firm', 'complain_form');
$this->db->where('complain_form.InstituteId', 'vf_training_firm.FirmId');
$this->db->where('complain_form.DistrictId', 'vf_training_district.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();
getting error of unknown column complain_form.InstituteId. each and every column is same as in db
Hope this helps you :
$this->db->select(*);
$this->db->from('complain_form cf');
$this->db->join('vf_training_firm vftf', 'vftf.FirmId = cf.InstituteId');
$this->db->join('vf_training_district vftd', 'vftd.DistrictId = cf.DistrictId');
$query = $this->db->get();
return $result = $query->result_array();
for more : https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
You need JOIN:
SELECT *
FROM complain_form
JOIN vf_training_firm ON complain_form.InstituteId = vf_training_firm.FirmId
JOIN vf_training_district ON complain_form.InstituteId = vf_training_district.DistrictId
using Query Builder class Codeigniter:
$row = $this->db->select(*)
->from('complain_form')
->join('vf_training_firm', 'complain_form.InstituteId = vf_training_firm.FirmId')
->join('vf_training_district', complain_form.InstituteId = vf_training_district.DistrictId)
->get();
return $row->result_array();
i am trying to fetch data from database table in between two dates and it is having two more filter using 'OR' clause but in result i am getting data either between date or data fetched using 'OR' clause.I am posting here query in model if anyone knows solution please let me know.
Thanks in advance
$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$this->db->where( 'against = Recharge');
$this->db->or_where('against =','Rech_Commission');
$query = $this->db->get();
return $query->result();
I want date filter compulsory but want to fetch records against=Recharge or against='Rech_commission'but i am getting either using date range or against='Rech_Commission'
use
$where = '(against="Recharge" or against = "Rech_Commission")';
$this->db->where($where);
and you had date and date and (against = '' or against = '')
$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->where('date BETWEEN "'. date('Y-m-d', strtotime($startDate)). '" and "'. date('Y-m-d', strtotime($endDate)).'"');
$this->db->where('against="Recharge" OR against = "Rech_Commission")';
$query = $this->db->get();
return $query->result();
$this->db->select('*');
$this->db->from('Transaction');
$this->db->join('Users', 'Transaction.user_id = Users.id');
$this->db->group_start();
$this->db->where('date >=',$startDate);
$this->db->where('date <=',$endDate);
$this->db->where( 'against = Recharge');
$this->db->group_end();
$this->db->or_where('against =','Rech_Commission');
$query = $this->db->get();
return $query->result();
I am trying to convert this query:
SELECT * FROM table WHERE condition1 = 'example' AND (date1 = 'date' OR renewal1 = 'renewal');
into CodeIgniter's Active Record format. I tried the following:
$q = $this->db->select('*');
$q = $this->db->from('table');
$q = $this->db->where('condition1 = condition');
$q = $this->db->where('date1 = date OR renewal = renewal');
$q = $this->db->get();
$results = $q->result();
But this did not have the desired affect. It seemed to not place the parenthesis around the second set of conditions, which caused the query to not work as expected. How else can I do this to represent what I want?
Thanks for the help!
You can use
$this->db->select('*');
$this->db->from('table');
$this->db->where('condition1 =',' condition');
$where = '(date1 = "date" OR renewal1 = "renewal")';// you can use your condition like this
$this->db->where($where);
$q = $this->db->get();
$results = $q->result();
I'm trying to...
Set some query parameters and filters.
Get the total number of matching rows.
Set limit and offset.
Get the results
Here's what I initially tried:
$this->db->select(<aggregation, subqueries>);
$this->db->from('users');
$this->db->where(<complex filters>);
$total = $this->db->count_all_results();
$this->db->limit($limit, $offset);
$query = $this->db->get();
$result = $query->result();
But calling count_all_results calls _reset_select internally meaning I have to do the first step again - not very DRY.
How would you achieve this in a simple, clean way?
Just use Query Builder Caching
$this->db->start_cache();
$this->db->select(<aggregation, subqueries>);
$this->db->from('users');
$this->db->where(<complex filters>);
$this->db->stop_cache();
$total = $this->db->count_all_results();
$this->db->limit($limit, $offset);
$query = $this->db->get();
$this->db->flush_cache();
$result = $query->result();
https://codeigniter-30-dev.readthedocs.io/zh_TW/latest/database/query_builder.html#this-db-count-all-results
Just want to get user_id and review_text values from table reviews, select name and city from table users where id=user_id and then return review_text, username and city to controller. Please help me.
public function did_get_reviews($item_id){
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$user_id = 'user_id' //??
$this->db->select('name','city');
$this->db->from('users');
$this->db->where('id', $user_id);
$query = $this->db->get('review_text','username','city'); //??
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
Update 1:
I just tried to join tables but it returns the values for review_text only but I also want to get values for name and city. Please check the code below:
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text','users.name','users.city');
$this->db->from('reviews','users');
$this->db->where('reviews.post_id', $item_id);
$this->db->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
i think you need to use join query in SQL. if you use this code you can access to what you want
$result = $this->db->select('review, username, city')
->from('reviews')
->join('city', 'city.user_id = review.user_id')
->get();
print_r($result);
for more information about how you can write join query(left, inner or right) in codeigniter you can see this link
i hope that this code solve your problem
UPDATE
in your new question. your code have a little buggy. you need to write your select in this way
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text,users.name,users.city')
->from('reviews','users')
->where('reviews.post_id', $item_id)
->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
in codeigniter select Active record; any column name separate with each other by , only (not by ' and ,)
in codeigniter documentation wrote that
Permits you to write the SELECT portion of your query:
$this->db->select('title, content, date');
$query = $this->db->get('mytable');
// Produces: SELECT title, content, date FROM mytable
You can get the value for user_id using:
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$result = $this->db->get();
print_r($result);
use the same $this->db->get(); to get the values of the second query as well. Once you get the values inside a variable you can iterate it. Something like:
foreach ($result->result() as $row)
{
echo $row->name;
echo $row->city;
}