0

I got the following Mysql command:

// Call on a random style ID to display in rating window which user hasn't seen yet.

$resultSet = $conn->query("SELECT pictureID,userID FROM styles WHERE NOT 
viewedByUser = (NOT LIKE '%$userID%')  ORDER BY RAND() LIMIT 1");
while($rows = $resultSet->fetch_assoc() ){
    $rateableUserID = $rows['userID'];
    $rateablePictureID = $rows['pictureID'];
}

I want to use the WHERE NOT function to search through the field "viewedByUser" after a string that does not contain the same string as the variable $userID.

What options have I got here?

1
  • You are using double negation and you've got a syntax error too (= (NOT LIKE). Try to simply use the NOT LIKE operators. Commented Aug 10, 2017 at 7:56

3 Answers 3

1

Try This :-

$resultSet = $conn->query("SELECT pictureID,userID FROM styles WHERE
viewedByUser NOT LIKE '%$userID%'  ORDER BY RAND() LIMIT 1");
while($rows = $resultSet->fetch_assoc() ){
    $rateableUserID = $rows['userID'];
    $rateablePictureID = $rows['pictureID'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can directly use != instead of NOT LIKE and there is nothing called WHERE NOT

$resultSet = $conn->query("SELECT pictureID, userID FROM styles WHERE viewedByUser != $userID ORDER BY RAND() LIMIT 1");

Since they are email addresses, you can do:

$userId = '[email protected] [email protected]';
$userId = explode(' ', $userId);
echo $userStr = implode("', '", $userId);
$resultSet = $conn->query("SELECT pictureID, userID FROM styles WHERE viewedByUser NOT IN ('" . $userStr . "') ORDER BY RAND() LIMIT 1");    

1 Comment

but the field 'viewedByUser' contains a long string with many emailadress which are seperated with a space. It doesn't recognise the variable $userID in that field and continious with the picked pictureID and userID :/
0

If you need a (not) like on the viewedByUser you can simply use

"SELECT pictureID,userID 
 FROM styles W
 WHERE viewedByUser = NOT LIKE '%$userID%' 
 ORDER BY RAND() 
LIMIT 1";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.