<?php
// connect to db
require_once 'mysql.php';
$connection = new mysqli($hn, $un, $pw, $db);
if ($connection->connect_error) die("Whoops.");
// check token
$token = $_REQUEST[token];
$query = "SELECT * FROM newusers WHERE token='$token'";
$result = $connection->query($query);
if (!$result)
{
echo "Nothing to activate.";
die("<meta http-equiv='refresh' content='3; url=index.php'>");
}
else ($result->num_rows)
{
$row = $result->fetch_array(MYSQLI_NUM);
$result->close();
$status = $row[4];
// check status
if ($status == "active")
{
// already active
}
else if ($status === "" || $status === NULL)
{
$query = "UPDATE newusers SET status='active' WHERE token='$token'";
$connection->query($query);
echo "Activated.";
die("<meta http-equiv='refresh' content='5; url=account.php'>");
}
}
echo "Why are you here?";
die("<meta http-equiv='refresh' content='3; url=index.php'>");
$connection->close();
?>
这样可以激活帐户吗?默认情况下,状态列设置为NULL ...我不确定是否可以使用精确的运算符将变量与NULL进行比较。
答案 0 :(得分:1)
要回答您的问题:
如果mysql对于该字段具有真实的NULL
,实际上将为$status = $row[4];
分配真实的NULL
以便php进行显式比较。因此,$status === NULL
实际上可以工作(但仅当mysql字段为空值时才有效)。
不过,根据您的代码,您所需要做的只是:
if ($row[4] == "active") {
// its active
} else {
// its not, so activate/do more
}
答案 1 :(得分:0)
您可以将empty用于此类事情。它检查null
,false
,0
,''
甚至空数组。
// change
else if ($status === "" || $status === NULL)
// to
else if (empty($status))