1

I have database with users, I want to show results without users that i have id's in array. How should i do it, any ideas?

<?php
include_once "dbh.php";
session_start();
$id = $_SESSION['userId'];

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<?php
$noid = array("8","9","10");
$sql = "SELECT * FROM users WHERE idUsers != '$noid' ORDER BY RAND() LIMIT 1; ";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result); 
if ($resultCheck> 0){
$row = mysqli_fetch_assoc($result);
echo $row['nick'];
}

?>

</body>
</html>
zonio
  • 15
  • 2

2 Answers2

-2

You can use not in,

"SELECT * FROM users WHERE idUsers not in(" . join(",",$noid) . ") ORDER BY RAND() LIMIT 1; "
LF00
  • 27,015
  • 29
  • 156
  • 295
-2

Your query should be:

'SELECT * FROM users WHERE id NOT IN (' . implode(',', $noid) . ')'; 

IF you have 'LIMIT 1' there you'll only get one result back.