CodeIgniter Forums
something causing a server error (500) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: something causing a server error (500) (/thread-70448.html)



something causing a server error (500) - richb201 - 04-10-2018

I have been battling this thing for a few weeks. I kept thinking the problem with the server error was my my js code running in the browser. But it seems that the CI backend is the problem. 
PHP Code:
       $table = array();
 
       if ($this->input->is_ajax_request()) {
 
           $json $this->input->raw_input_stream;
 
           $json json_decode($json);
 
           $email_key $json->email;
 
      //     $sql="Select campaign FROM employees WHERE employee_email=?";
 
       //    $this->db->query($sql,array(1,$email_key));
 
       //    $camp = $this->db->get()->result();
 
           $query=$this->db->get_where('employees', array('employee_email'=>$email_key))->result();
 
           if ($query->num_rows() > 0)   <<<<<This is the line that causes the server error
            
{
 
               $campaign=$query->result();
 
           }
 
           else
            
{
 
               $this->db->close();  //no campaign for this guy
 
               $this->output
                    
->set_content_type('application/json')
 
                   ->set_output(json_encode("user not found"));
 
               return;
 
           
Something is wrong with the num_rows call. The get_where seems to work OK. What am I missing here?


RE: something causing a server error (500) - jreklund - 04-10-2018

If I'm not mistaken, you can't call num_rows after you have returned a result.
PHP Code:
$query $this->db->get_where('employees', array('employee_email'=>$email_key));
if (
$query->num_rows() > 0)
{
    
$campaign $query->result();




RE: something causing a server error (500) - richb201 - 04-10-2018

(04-10-2018, 12:58 PM)jreklund Wrote: If I'm not mistaken, you can't call num_rows after you have returned a result.
PHP Code:
$query $this->db->get_where('employees', array('employee_email'=>$email_key));
if (
$query->num_rows() > 0)
{
    
$campaign $query->result();


That was it. thx


RE: something causing a server error (500) - richb201 - 04-10-2018

Well that stopped it from crashing.


 I am trying to return the field $campaign. Here is the code
          $query=$this->db->get_where('employees', array('employee_email'=>$email_key));

       if ($query->result_id->num_rows > 0)
       {
           $camp=$query->result()->$campaign;  
       }


I thought this would do it but it is returning $camp=null. There are two rows where employee_email=$email_key, so num_rows=2.  But how do I reposition the pointer to point at the first row and then get a single value?


RE: something causing a server error (500) - richb201 - 04-10-2018

solved. I used:

$row=$query->row();
$camp=$row->campaign;

row() will return the first row