Welcome Guest, Not a member yet? Register   Sign In
something causing a server error (500)
#1

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?
proof that an old dog can learn new tricks
Reply
#2

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();

Reply
#3

(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
proof that an old dog can learn new tricks
Reply
#4

(This post was last modified: 04-10-2018, 03:27 PM by richb201.)

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?
proof that an old dog can learn new tricks
Reply
#5

solved. I used:

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

row() will return the first row
proof that an old dog can learn new tricks
Reply




Theme © iAndrew 2016 - Forum software by © MyBB