Master
Master

Reputation: 37

Show all users whose status is "user" or "admin" on the list

I have an issue to get users data by their status (Admin/User).

I don't want to show an admin on the list, so help me...

 $status= 'user';

// I've tried this, but it didn't helped

$sql = "SELECT * FROM users WHERE status = $status";

$result = $conn->query($sql);

if ($result->num_rows >0) {


while($row[] = $result->fetch_assoc()) {

 $tem = $row;

 $json = json_encode($tem);

 }

} else {
 echo "Error";
}
 echo $json;

$conn->close();
 }
?>

Upvotes: 0

Views: 181

Answers (1)

godot
godot

Reputation: 3545

You need to put $status in the single quotation marks


if

$status = 'user';

then:

$sql = "SELECT * FROM users WHERE status = '$status'";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {


while($row = mysqli_fetch_assoc($result)) {



 $json = json_encode($row);

 }

} else {
 echo "Error";
}
 echo $json;

$conn->close();
 }
?>

Upvotes: 1

Related Questions