开发者

why does this php code not work?

开发者 https://www.devze.com 2023-04-11 11:05 出处:网络
I have a BOOLEAN in my table, set to 0 or 1. $test = 开发者_StackOverflow中文版mysql_query(\"SELECT status FROM mydb WHERE email = \'admin@mail.com\'\") or die(mysql_error());

I have a BOOLEAN in my table, set to 0 or 1.

$test = 开发者_StackOverflow中文版mysql_query("SELECT status FROM mydb WHERE email = 'admin@mail.com'") or die(mysql_error());

if ($test == 0) { 
    echo "FF";
} elseif ($test == 1) { 
    echo "ON";
}

But this doesn't work, why? I tried true and false, that doesn´t work either.


You are not actually fetching the result.

$test = mysql_query("SELECT status FROM mydb WHERE email = 'admin@mail.com'");
if (!$test) {
    die(mysql_error());
} else {
    $res = $test;
}
$row = mysql_fetch_assoc($res);

var_dump($row);


$test is just the results from the query. You want to see if you get a row try this:

$result = mysql_query("SELECT status FROM mydb WHERE email = 'admin@mail.com'")or die(mysql_error());

$row_count = mysql_num_rows($result);

if($row_count == 0)
{
  echo "FF";
}
else if($row_count == 1)
{
  echo "ON";
}

If you need to know the value of status

$result = mysql_query("SELECT status FROM mydb WHERE email = 'admin@mail.com'")or die(mysql_error());

 while($data = mysql_fetch_assoc($result))
 {
    if($data['status'] == 0)
    {
      echo "FF";
    }
    else if($data['status'] == 1)
    {
      echo "ON";
    }
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消