1

Depending on whether any results are found or not I want to set a value to $case_num and store it in an array to pass back to an AJAX call using json. The code I am using works as expected if an entry is found but if there are no results the value passed back is simply null.

$sql = "SELECT * FROM cases WHERE name LIKE '%$serial%' 
        AND DATE(date_entered) = CURDATE()";

$result_case = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_array($result_case);

 if(!empty($result_case)){
      $case_id = $row['id'];
      $case_num = $row['name'];
    }else{
      $case_num = 'not found';
 }

$output_array = array(
    'source' => $source,
    'case_found' => $case_num
);

echo json_encode($output_array);
2
  • What do you want $case_num to be if there are no results? Commented May 20, 2016 at 19:07
  • 3
    $result_case will never be empty, NEVER. Commented May 20, 2016 at 19:07

2 Answers 2

2

You should change your if condition

if($row)

Remember that mysqli_fetch_array will return NULL if there's no rows to fetch and mysqli_query returns a boolean based on if the query succeeded (as in generated no errors)

Sign up to request clarification or add additional context in comments.

Comments

1

$result_case arent null, if no have result, the value is false, you can try use like (isset($row)) or $result_case!=false

1 Comment

$row will be set regardless. So isset($row) is a useless test

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.