Use like in join in codeigniter - codeigniter

public function get_rows_set_test( $table, $id_subjects )
{
$this->db->select('id_sets','sets.sets_name');
$this->db->from($table);
$this->db->join('sets', 'sets.id LIKE CONCAT("%",questions.id_sets,"%")');
$this->db->where('id_subjects' , $id_subjects);
$this->db->distinct();
$query = $this->db->get();
if( $query->num_rows() > 0 )
{
foreach( $query->result() as $row ) $rows[] = $row;
return $rows;
}
else return '';
}
its gives this error
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIKE CONCAT("%",questions.id_sets,"%") WHERE id_subjects = '13'' at line 3
SELECT DISTINCT id_sets FROM (questions) JOIN sets ON sets.id LIKE CONCAT("%",questions.id_sets,"%") WHERE id_subjects = '13'
Filename: C:\xampp\htdocs\modeltest\system\database\DB_driver.php
Line Number: 330
error

$this->db->join('sets', 'sets.id LIKE CONCAT("%",questions.id_sets,"%")', false);

Please check below function with join between two tables:
public function get_rows_set_test( $table, $id_subjects )
{
$this->db->select('id_sets','sets.sets_name');
$this->db->from($table);
$this->db->join('sets','sets.id=questions.id_sets');
$this->db->where('id_subjects' , $id_subjects);
$this->db->distinct();
$query = $this->db->get();
if( $query->num_rows() > 0 )
{
foreach( $query->result() as $row ) $rows[] = $row;
return $rows;
}
else return '';
}

You don't need the LIKE there, you are matching the two id I suppose, simple do the following
$this->db->join('sets','sets.id=questions.id_sets');

You could write it as a stored procedure on the database side then use:
`$query = $this->db->query("call stored_procedure()");'
https://codeigniter.com/user_guide/database/call_function.html

Related

codeigniter : Commands out of sync; you can't run this command now

I applied all the possible answers but still same problem.
also tried
$this->db->reconnect();
there is no problem in my query
MyCode:
public function GetdistributorsDetails($username){
$sql = "SELECT u.FirstName, u.Email, u.Telephone, u.MobileNumber, u.AlternateMobileNumber, ud.Address1, ud.Pincode,ud.City,s.Statename FROM users u JOIN userdetails ud ON ud.UserId = u.UserId JOIN states s ON s.StateId = ud.StateId WHERE u.Username = ? ";
$result = $this->db->query($sql,array($username));
return $result->result_array();
}
add following code into /system/database/drivers/mysqli/mysqli_result.php
function next_result()
{
if (is_object($this->conn_id))
{
return mysqli_next_result($this->conn_id);
}
}
then in model when you call Stored Procedure
$query = $this->db->query("CALL test()");
$res = $query->result();
//add this two line
$query->next_result();
$query->free_result();
//end of new code
return $res;
you can use this after call
mysqli_next_result( $this->db->conn_id );
Here is example if you don't want change any thing in mysqli drivers files
$sql = "CALL procedureName(IntParam,'VarcharParm1','VaracharParm2','VarcharParm3');";
$query = $this->db->query($sql);
mysqli_next_result( $this->db->conn_id );
$query->free_result();
these are important line place after to remove the error
mysqli_next_result( $this->db->conn_id );
$query->free_result();
Just use this one if you use multiple query make in same function:
$this->db->close();
If your stored procedure returns more than one result, try to add this code between the queries:
$storedProcedure = 'CALL test(inputParam, #outputParam)';
$this->db->query($storedProcedure);
$conn = $this->db->conn_id;
do {
if ($result = mysqli_store_result($conn)) {
mysqli_free_result($result);
}
} while (mysqli_more_results($conn) && mysqli_next_result($conn));
$sql = 'SELECT #outputParam;';
$this->db->query($sql);
Just add next_result function in
system\database\drivers\mysqli\mysqli_result.php file
public function next_result(){
if(is_object($this->conn_id) && mysqli_more_results($this->conn_id)){
return mysqli_next_result($this->conn_id);
}
}
To call query function
$result = $this->db->query("CALL {$Name}({$Q})",$Param);
$result->next_result();
return $result;

I couldnt get the joined table value in my query

Workflow type table
[Workflow caterory table][2]
public function view_wf_category()
{
$this->db->select('workflow_category.wf_cat','workflow_type.type_name');
$this->db->from('workflow_category');
$this->db->join('workflow_type', 'workflow_type.workflow_type_id = workflow_category.wf_type');
$query = $this->db->get();
foreach($query->result_array() AS $row)
{
echo $row["wf_cat"]."<br/>";
echo $row["type_name"]."<br/>";
}
}
here im not able to print the $row["type_name"] and also showing a notice as undefined index for $row["type_name"].
Try like this...
public function view_wf_category()
{
$this->db->select('workflow_category.wf_cat,workflow_type.type_name')
->from('workflow_category')
->join('workflow_type', 'workflow_type.workflow_type_id = workflow_category.wf_type');
$query = $this->db->get();
foreach($query->result_array() as $row)
{
echo $row["wf_cat"]."<br/>";
echo $row["type_name"]."<br/>";
}
}
Error due to this line..
$this->db->select('workflow_category.wf_cat','workflow_type.type_name');
So it should be just
$this->db->select('workflow_category.wf_cat,workflow_type.type_name');
In above query i have been used query grouping.

Combining results into one if multiple

I have these two functions below
public function get_posts() {
$this->db->select('p.post_id, p.reply_id, p.user_id, u.username');
$this->db->from('post as p');
$this->db->join('user as u', 'u.user_id = p.user_id');
$this->db->where('p.post_id', $this->input->get('post_id'));
$this->db->or_where('p.reply_id', $this->input->get('post_id'));
// $this->db->group_by('p.user_id');
$query = $this->db->get();
return $query->result_array();
}
Results Output
SELECT * FROM `post` WHERE `post_id` = '1' AND `reply_id` = '0' AND `user_id` = '2'
SELECT * FROM `post` WHERE `post_id` = '3' AND `reply_id` = '1' AND `user_id` = '1'
SELECT * FROM `post` WHERE `post_id` = '5' AND `reply_id` = '1' AND `user_id` = '1'
Total Posts Function
public function total_posts_by_user($user_id, $reply_id, $post_id) {
$this->db->from('post');
$this->db->where('post_id', $post_id);
$this->db->where('reply_id', $reply_id);
$this->db->where('user_id', $user_id);
// $this->db->group_by('user_id');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->num_rows();
}
}
The total_posts_by user gets the number of post shown in image below
Question As you can see there are 2 admin row results showing. But I
would like them to be combined so it will just say row for admin and 2
posts.
I have tried using group_by() on get_post but dos not work
Controller
<?php
class Who_replied extends MX_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['posts'] = array();
$results = $this->get_posts();
foreach ($results as $result) {
$data['posts'][] = array(
'username' => $result['username'],
'total' => $this->total_posts_by_user($result['user_id'], $result['reply_id'], $result['post_id'])
);
$this->total_posts_by_user($result['user_id'], $result['reply_id'], $result['post_id']);
echo $this->db->last_query() . '</br>';
}
$data['total_posts'] = $this->total_posts();
return $this->load->view('default/template/forum/categories/who_replied_view', $data);
}
}
Use left join in first method because current it is using inner join. After that use group by
public function get_posts() {
$this->db->select('p.post_id, p.reply_id, p.user_id, u.username');
$this->db->from('post as p');
$this->db->join('user as u', 'u.user_id = p.user_id','left');
$this->db->where('p.post_id', $this->input->get('post_id'));
$this->db->or_where('p.reply_id', $this->input->get('post_id'));
$this->db->group_by('p.post_id');
$query = $this->db->get();
return $query->result_array();
}
and try. If still face same issue double check whether there are same TWO username admin
** Have you tried DISCTINCT?
hope it will help
I have found my solution from here using COUNT(p.user_id) AS total
public function get_posts() {
$this->db->select('p.post_id, p.reply_id, p.user_id, u.username COUNT(p.user_id) AS total');
$this->db->from('post as p');
$this->db->join('user as u', 'u.user_id = p.user_id');
$this->db->where('p.post_id', $this->input->get('post_id'));
$this->db->or_where('p.reply_id', $this->input->get('post_id'));
$this->db->group_by('p.user_id');
$query = $this->db->get();
return $query->result_array();
}

Call to a member function row() on a non-object

i am getting error Call to a member function row() on a non-object in codeigniter my controller is
public function edit_survey_pro($id)
{
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (sizeof($survey) == 0) $this->template->error(lang("error_32"));
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
}
my model is
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
{
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
$this->db->limit($perpage,$start);
if($where){
$this->db->where($where);
}
if($order_by){
$this->db->order_by($order_by);
}
if($arr=='')
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
if(!empty($query))
if($perpage != 0 && $perpage != NULL)
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
here loadContent() is just load the content with view path
public function loadContent($view,$data=array(),$die=0){
//something to load the content
}
in my model I am getting the result as an array of object in $query and then it is returned as $result like this -
$query = $this->db->get()->result(); but at the controller $survey stores array of object and i want to show the content of that array of object ,previously I use
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey->row()
)
);
to get that data but the problem is $survey->row() cannot return that data bcoz it is not an object it is array of object so it can't be returned through row() method
so instead of this I just call the first element of that data like this-
$this->template->loadContent("user/edit_survey_pro", array(
"survey" => $survey[0]
)
);
Somehow its works for me bcoz I want to show the first row of the data
if sembody wants to show all data then I think he shuld try logic to increment the key value of that array of object for me it is $survey[] you can use foreach loop for increment the of value of the key element
The problems i see are your model, I will dissect it and add comments to your original code to point out the issues:
function get($table,$where='',$perpage=0,$start=0,$order_by='',$arr='')
//above there are problems, you are setting some of your parameters to
//equal blank, but below, in your conditionals, you are checking if they
// exist. They will always exist if they are set to blank. Fix them by
// setting them = NULL like this:
// get($table,$where=null,$perpage=0,$start=0,$order_by=null,$arr=null)
{
$this->db->select();// <-- you forgot this
$this->db->from($table);
if($perpage != 0 && $perpage != NULL)
//when will $perpage = null? , if never, then you dont need it.
$this->db->limit($perpage,$start);
if($where){
//change this to if(isset($where)). Also why do you use
//curly braces here, but not in the above if statement if only
//one line is affected in your if. I would remove the
//curly braces here.
$this->db->where($where);
}
if($order_by){
// change this to if(isset($order_by)). Same thing as
//above about the curly braces here
$this->db->order_by($order_by);
}
if($arr=='')
// change this to if(isset($arr)).
$query = $this->db->get()->result();
else
$query = $this->db->get()->result('array');
//change this to: $query = $this->db->get()->result_array();
if(!empty($query))
//change the above to if($query->num_rows > 0). Also, here since
//your code body is longer then one line, you will need curly braces
//around your if statement
if($perpage != 0 && $perpage != NULL)
//again, will $perpage ever be NULL? However, why do you need
//this conditional at all, if the limit above is already
//doing this job?
$result = $query;
else
$result = $query[0];
else
$result = array();
return $result;
}
after applying all the changes:
MODEL:
function get($table, $where=null, $perpage=0, $start=0, $order_by=null, $arr=null)
{
$this->db->select();
$this->db->from($table);
if($perpage != 0)
$this->db->limit($perpage, $start);
if(isset($where))
$this->db->where($where);
if(isset($order_by))
$this->db->order_by($order_by);
if(isset($arr)) {
$result = $this->db->get()->result_array();
} else {
$result = $this->db->get()->result();
}
return $result;
}
CONTROLLER
public function edit_survey_pro($id) {
$id = intval($id);
$survey = $this->model->get("surveys",array("ID" => $id),100000);
if (!$survey) {
$this->template->error(lang("error_32"));
} else {
$data["survey"] = $survey;
$this->template->loadContent("user/edit_survey_pro", $data);
}
}
I think when you use $this->db->get(), you need to pass the table name as param like this:
$this->db->get('table_name')->result();

getting error while passing a array through loop

This my controller function
public function update_monthly() {
$data = $this->student_model->get_due_info();
foreach ($data as $value) {
$due = $this->student_model->get_due_by_id($value);
$grade = $this->student_model->get_grade_by_id($value);
$grade_due = $this->student_model->get_due_by_grade($grade);
$new_due = $due + $grade_due;
$this->db->set('md_due',$new_due, FALSE);
}
}
I am getting following error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_active_rec.php
Line Number: 427
and
A Database Error Occurred
Error Number: 1054
Unknown column 'Array' in 'where clause'
SELECT `md_due` FROM (`monthly_due`) WHERE `md_student_id` = Array
Filename: F:\xampp\htdocs\student\system\database\DB_driver.php
Line Number: 330
Model Code
public function get_due_by_id($id) {
$this->db->select('md_due');
$this->db->from('monthly_due');
$this->db->where('md_student_id', $id);
$query = $this->db->get();
return $query->result_array();
}
public function get_due_by_grade($id) {
$this->db->select('dt_fee');
$this->db->from('due_table');
$this->db->where('dt_grade', $id);
$query = $this->db->get();
return $query->result_array();
}
public function get_grade_by_id($id) {
$this->db->select('grade');
$this->db->from('student_info');
$this->db->where('student_gen_id', $id);
$query = $this->db->get();
return $query->result_array();
}
public function get_due_info() {
$this->db->select('md_student_id');
$this->db->from('monthly_due');
$query = $this->db->get();
return $query->result_array();
}
Pass $value['md_student_id'] to the model instead of $value in controller.
$data = $this->student_model->get_due_info();
foreach ($data as $value) {
$due = $this->student_model->get_due_by_id($value['md_student_id'] );
$grade = $this->student_model->get_grade_by_id($value['md_student_id']);
$grade_due = $this->student_model->get_due_by_grade($grade[0]['grade']);
//Add remaining logic
}
you are passing whole the array to your model not just the value you want
$due = $this->student_model->get_due_by_id($value);
the right thing is to pass
$value['md_student_id']
this is the value you actually want to pass
replace your code with this
foreach ($data as $value) {
$due = $this->student_model->get_due_by_id($value['md_student_id'] );
$grade = $this->student_model->get_grade_by_id($value['md_student_id']);
$grade_due = $this->student_model->get_due_by_grade($grade[0]['grade']);
$new_due = $due + $grade_due;
$this->db->set('md_due',$new_due, FALSE);
}

Resources