codeigniter join and session - codeigniter

I'm trying make a chat app for my website..But i can't take the messages for member who login..Session is not working with join..How can i fix it ?
My model
public function masajlarigoster(){
$_session=$this->session->userdata('logged_in');
$query=$this->db->query('select cekid.cekId,mesajlar.*
from cekid left join mesajlar on (mesajlar.kimId=$_session->Id
and mesajlar.kimeId=cekid.cekId)
or (mesajlar.kimId=cekid.cekId
and mesajlar.kimeId=$_session->Id) ');
if($query->num_rows() > 0){
return $query->result();
}else{
return false;
}
}

$_SESSION is a protected global. You are also trying to use an array as an object...
You can use the query builder for this if you have it enabled (disclaimer: i haven't tested this and have done it from memory)
$loggedin = $this->session->get_userdata('logged_in');
$query = $this->db->select('cekid.cekId,mesajlar.*')
->from('cekid')
->join('mesajlar','mesajlar.kimeId=cekid.cekId','left')
->group_start()
->where('mesajlar.kimId', $loggedin)
->or_group_start()
->where('mesajlar.kimId', 'cekid.cekId')
->where('mesajlar.kimeId', $loggedin)
->group_end()
->group_end()
->get();
if($query->num_rows() > 0){
return $query->result();
}else{
return false;
}

Related

search in codeigniter if my input data is match either not

i want to search in codeigniter if my input data is matched then shows result but couldn't match couldn't shows data..
Here is my model:
public function get_search(){
$p_cat = $this->input->post('p_category');
$p_place = $this->input->post('p_place');
$p_price = $this->input->post('p_price');
$search = $this->input->post('search');
$this->db->like('p_category',$p_cat,$search);
$this->db->or_like('p_place',$p_place,$search);
$this->db->or_like('p_price',$p_price,$search);
$query = $this->db->get('tbl_property');
if($query->num_rows() > 0)
return $query->result_array();
else
return FALSE;
}
But this model result match or mismatch it shows any data...
You have issue in your or_like method calling of query builder class. It should be like:
public function get_search(){
// Below three lines is not useful until you are using any other conditions to fetch data from DB
$p_cat = $this->input->post('p_category');
$p_place = $this->input->post('p_place');
$p_price = $this->input->post('p_price');
// I guess this string should be search in three columns
$search = $this->input->post('search');
$this->db->like('p_category',$search);
$this->db->or_like('p_place',$search);
$this->db->or_like('p_price',$search);
$query = $this->db->get('tbl_property');
if($query->num_rows() > 0)
return $query->result_array();
else
return FALSE;
}
Let me know if you still face any issue.

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;

retrieve data from database in for loop

my controller is
function shiftstudentdetailsmultiple(){
$data['Show'] = $this->input->post('show[]');
$roll = $this->input->post('roll[]');
//echo sizeof($data1);
for($i=0;$i<sizeof($roll);$i++)
{
$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
}
$this->load->view('registrar/shift_student_view', $data);
}
and model is
function getstudentsdetailscouseandmultiple($Uniq_Id){
$this->db->select("sprd.Uniq_Id, sprd.Name, sprd.Uni_Roll_No, cd.Course_Name, bd.Branch_Name, sprd.Year, sprd.Session, sprd.Section, sprd.Fee_Status, sprd.Curr_Status");
$this->db->where("sprd.Uniq_Id",$Uniq_Id);
$this->db->from("Students_Prim_Detals sprd");
$this->db->join('Course_Details cd', 'cd.Course_Id=sprd.Course_id');
$this->db->join('Branch_Details bd', 'bd.Branch_Id=sprd.Branch_id');
$query = $this->db->get();
if ($query->num_rows() > 0){
return $query->result();
}
else {
return false;
}
}
when showing in view the fetching data showing only one time.. but in my database i have more then 4 entry when i print_r
$data['results']=this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
the data showing correct .. when load only showing one data..the last entry..
$this->load->view('registrar/shift_student_view', $data);
Each time you call...
$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
you replace the value stored in $data['results'] with the new return. So only one return is sent to the view.
You will need to revise your controller along these lines.
$student_detail = array();
for($i=0;$i<sizeof($roll);$i++)
{
$student_detail[] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);
}
$data['results'] = $student_detail;
Then in your view you will have to loop through $results to display each item in the array.
foreach($results as $detail){
echo "Name: ". $detail->name;
echo "Course: ". $detail->Course_Name;
//etc...
}

Codeigniter get row

I use codeigniter for my website, but I have a problem I have done script who returns rows from different tables. But it write me error Trying to get property of non-object. This is my code. Where is a problem?
$this->db->select('*');
$this->db->from('orders');
$this->db->where('order_id',$order_id);
$array_keys_values = $this->db->get();
$row = $array_keys_values->row();
$this->db->select('*');
$this->db->from('pacients');
$this->db->where('pacient_account_id',$row->order_pacient_id);
$array_keys_values2 = $this->db->get();
$row2 = $array_keys_values2->row();
$this->db->select('*');
$this->db->from('doctors');
$this->db->where('doctor_account_id',$doctor_id);
$array_keys_values3 = $this->db->get();
$row3 = $array_keys_values3->row();
Try
$this->db->select('*');
$this->db->from('orders');
$this->db->where('order_id',$order_id);
$array_keys_values = $this->db->get();
if ($array_keys_values->num_rows() > 0) {
foreach ($array_keys_values->result() as $row) {
// now you can work with $row
}
}
$this->db->select('*');
$this->db->from('pacients');
$this->db->where('pacient_account_id',$row->order_pacient_id);
$array_keys_values2 = $this->db->get();
if ($array_keys_values2->num_rows() > 0) {
foreach ($array_keys_values2->result() as $row2) {
// now you can work with $row2
}
}
$this->db->select('*');
$this->db->from('doctors');
$this->db->where('doctor_account_id',$doctor_id);
$array_keys_values3 = $this->db->get();
if ($array_keys_values3->num_rows() > 0) {
foreach ($array_keys_values3->result() as $row3) {
// now you can work with $row3
}
}
No row was returned. Use an if statement with a num_rows() > 0 to check

Codeigniter: Can I return multiple values from the same function?

I would like to return the query results along w/ the row count without having to run multiple queries. Is this possible in Codeigniter? The following works for returning the query results, but is it possible to also count the number of entries found?
Controller:
$data['records'] = $this->item_model->searchItem($item_name);
Model:
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
return $query->result();
$bindings = array($this->user_id, '%'.$item_name.'%');
$records = $this->db->query($sql, $bindings)->result();
return array(
'records' => $records,
'count' => count($records),
);
Then, in your controller:
$query = $this->item_model->searchItem($item_name);
$data['records'] = $query['records'];
$data['count'] = $query['count'];
Option one
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
$data['result'] = $query->result();
$data['rows'] = $query->num_rows();
return $data;
Option two
// model
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
return $query;
// controller
$data['query'] = $this->item_model->searchItem($item_name);
// then you have $data['query']->result();
// and $data['query']->num_rows();
You can send as many variables from a function as you want ..However plz remember that a function is supposed to do one UNIT of work . thus this rule must not be violated.
we can used contrel structure
if this {return A;} elseif () {return B;} else {return C;}
ALso we can send (bundle) variables in an array and send

Resources