سؤال

Good day fellow programmers! For some weird reason the function will not return anything after the first query is executed. I noticed the $error variable was not being returned and after checking I noticed nothing would be returned after the first query is executed. Any ideas why this might be happening?

public function store_review(){
  $this->load->database();
  $error = NULL;

  $title = $this->input->post('title');   
  $raiting = $this->input->post('raiting');
  $description = $this->input->post('description');

  //Insert review
  $this->db->query("INSERT INTO reviews (title, raiting) VALUES (?, ?)", array($title, $raiting));    

  if ($this->db->affected_rows() == 1){
    $reviewID = $this->db->insert_id();

    //If review inserted insert description             
    $this->db->query("INSERT INTO reviews_descriptions (review_id, description) VALUES (?, ?)", array($reviewID, $description));

    if ($this->db->affected_rows() != 1){
      $error = 'Could not insert review description.';
    }           
  } 
  else {
    //If review could not be inserted duplicate exists
    $error = 'Review already exists.';
  }

  //THE FUNCTION WILL NOT EXECUTE ANY RETURNS AFTER THE FIRST QUERY
  return $error;
}

NOTE: I am using CodeIgniter.

لا يوجد حل صحيح

نصائح أخرى

Here is the catch:

 $error = NULL;

when you assign NULL to $error it makes $error a set variable, eventhough it is null.

Thus the following line would give you unexpected result:

$error = $this->model->store_review(); if(isset($error)) echo 'error is set';

The proper way to check would be to use is_null


Here is how you can test my answer

<?php

$error = NULL;

if(is_null($error)){

echo "is_null | error not set \n";
}else{

echo "is_null |error set \n";
}


if(isset($error)){

echo "isset | error not set \n";
}else{

echo "isset |error set \n";
}

OUTPUT

is_null | error not set 
isset   | error set 

DEMO

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top