0

I am retrieving the details from the database if the condition is satisfied. But when the condition is not satisfied, empty result is retrived. So how to have a condition if status is empty

$sq = mysqli_query($link, $query);
$ro = mysqli_fetch_array($sq);
$status = $ro['status'];

if ($status == 1) {
    header("location: paymentPage.php");
} elseif ($status == 0) {
    header("location:login.php");
} elseif ($status == '\0') {
    header("location:SignUp.php");
}

But I am not able to redirect to signup page if the status is empty.Is status=='\0' is correct to go for signup page.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Naga Bhavani
  • 13
  • 1
  • 6
  • I think this is an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You don't check if status is empty, you check if query returned something. I think you've disabled error reporting and don't really see that acessing `$ro['status']` fails when there are no results. – FirstOne Nov 01 '17 at 14:20
  • The result will be empty right if there is no record with that specified name. Then , can i directly access that if $res == "" – Naga Bhavani Nov 01 '17 at 14:33
  • I don't know what `$res` is, but there are better ways of checking if the query found any results than using `== ""`. Such as [Best way to check if MySQL results returned in PHP?](https://stackoverflow.com/questions/4286586/best-way-to-check-if-mysql-results-returned-in-php). **Warning: the linked question uses deprecated API, adapt it accordingly.** – FirstOne Nov 01 '17 at 14:34

2 Answers2

0

If your column status has integer type in DB then it can't have value '\0' because '\0' is a string. Maybe you should use is_null function? For example:

else if(is_null($status))
{
    header("location:SignUp.php"); 
}
hungl
  • 101
  • 8
0

In all case, mysqli_fetch_array function will return array|null|false types. It means that, if your connection didn't establish or data is empty, your response will return empty array or empty string|boolean item. Therefore, you can check if is exits by any case like that:

if( !($row = mysqli_fetch_array($sq)))
{
  // Data doesn't exists
  header("location:SignUp.php"); 
}else{
  $status = $ro['status'];
  echo 'My other validations';
}

To read documentation about function look at: https://www.php.net/manual/tr/mysqli-result.fetch-array.php

Tural Rzaxanov
  • 783
  • 1
  • 7
  • 16