我正在尝试使用php实现某些功能。我想在我的用户表上显示两个地址。我想显示一个地址(ID等于3)给某些特定的用户ID,另一个地址(ID等于4)给其他每个用户ID。在下面检查我的代码
<?php
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "user";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = "SELECT * FROM users WHERE id = '".$_SESSION['user']."'";
if ( ($id != 11303) && ($id != 27)) {
$sql = "SELECT address FROM admin WHERE id='3'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "" . $row["address"]. "";
}
} else {
echo "0 results";
}
}
else {
$sql = "SELECT address FROM admin WHERE id='4'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "" . $row["address"]. "";
}
} else {
echo "0 results";
}
}
$conn->close();
?>
我尝试从其会话中获取用户ID,然后使用“ if”检查其会话ID是否等于某些特定ID,然后从管理表中选择ID为3的地址,并显示为“ else”,并显示地址为id4。但它向所有人显示id为3的地址。请任何人能帮忙吗?
答案 0 :(得分:2)
在使用$_SESSION
之前,请在代码顶部写上start_session()
。
然后尝试:
$sql = mysqli_query($conn,"SELECT * FROM users WHERE id = '".$_SESSION['user']."'");
$result = mysqli_fetch_assoc($sql);
$id = $result['id'];
现在,将$id
的值设置为您的条件
if ( ($id != 11303) && ($id != 27))
{
//do something
}