$UserId = $_GET['UserId'];
$UserData = $con->query("SELECT * FROM ElectionBlacklist WHERE UserId=".$UserId) or trigger_error($mysqli->error);
while ($row = $UserData->fetch_array()) {
if (is_null($row[UserId])) {
echo json_encode(array(
'UserId' => 'nil',
)
);
} elseif ($row[UserId] == $UserId) {
echo json_encode(array(
'UserId' => $row[UserId],
)
);
} else {
echo json_encode(array(
'UserId' => 'nil',
)
);
}
}
我正在尝试返回UserID => nil如果查询没有返回任何内容,但是,当测试页面只是空白时意味着没有返回任何内容......
答案 0 :(得分:0)
也许这样试试?不要发送您的查询
$UserId = $_GET['UserId'];
if ($_GET['UserId']) {
$UserData = $con->query("SELECT * FROM ElectionBlacklist WHERE UserId=".$UserId) or trigger_error($mysqli->error);
while ($row = $UserData->fetch_array()) {
if (is_null($row[UserId])) {
echo json_encode(array(
'UserId' => 'nil',
));
} elseif ($row[UserId] == $UserId) {
echo json_encode(array(
'UserId' => $row[UserId],
));
} else {
echo json_encode(array(
'UserId' => 'nil',
));
}
}
} else {
echo json_encode(array('UserId' => 'nil'))
}
答案 1 :(得分:0)
然后您的代码没有进入while
循环...
您必须先检查row
var:
$UserData = $con->query("SELECT * FROM ElectionBlacklist WHERE UserId=".$UserId) or trigger_error($mysqli->error);
$row = $UserData->fetch_array();
if(empty($row))
{
echo json_encode(array(
'UserId' => 'nil',
));
}
else {
do{
if (is_null($row[UserId])) {
echo json_encode(array(
'UserId' => 'nil',
)
);
} elseif ($row[UserId] == $UserId) {
echo json_encode(array(
'UserId' => $row[UserId],
)
);
} else {
echo json_encode(array(
'UserId' => 'nil',
)
);
}
} while($row = $UserData->fetch_array())
}