i.e Say:
select
*
from tblname
where col1 NOT IN array;
have problem using NOT IN.... but no开发者_JS百科t when i do: col1 = 'singlevalue';
Please help
use select * from tblname where col1 NOT IN ('value1','value2',...,'valueX')
Post the code you have, this works (if I understood your question right):
select userid,username
from users
where userid NOT IN (1,2,3,4)
limit 10
you should:
- format you code
- exactly describe what's you problem; if there's an error message, please post that message
- provide as much reasonable information as you can, so it's easy for us to see what you're trying to do
EDIT: some more points:
- show the error-message you're getting
- output the sql-statement that gets executed and try to execute it by hand (using phpmyadmin something similar)
var_dump
orprint_r
your result and see what you get - if it'sfalse
, think about what could be wrong: either your sql returns zero rows or there's a failure in it - just one of those two possibilitys- if you get additional information, edit your question and list it there, don't force everybody to read every comment on every answer till now - you've given some information, but it's spread to everywhere
fetch_object
won't be the problem - the problem is, as said, either a result with zero rows or a bad statement
till now, this is just a comment, my attempt to answer your question (it's not realy clear whats your problem):
sounds like you havn't understood how NOT IN works and havn't looked at the documentation. you query should look like this:
SELECT * FROM tblname where col1 NOT IN (1, 2, 3);
# values of your array here ^^^^^^^
to build that from an php-array, you could use implode or something similar.
EDIT: a very basic sollution could look like this (not that you should check the input to avoid sql-injection and such stuff):
// for simple numerical values
$sql = "SELECT * FROM tblname where col1 NOT IN (".implode(", ",$myArray).")";
// for strings
$sql = "SELECT * FROM tblname where col1 NOT IN ('".implode("', '",$myArray)."')";
You likely have a NULL in your "array". That will cause NOT IN() to behave in a way you don't expect. Check for that.
It would help to see your exact query to be sure, but NOT IN is not followed by an array. its followed by (a, b, c) comma delimited string within parentheses
$inString = "(" . implode(", ", $array) . ")"; //looks like ('val1', 'val2', 'val3')
$sql = "SELECT * FROM tblname WHERE col1 NOT IN $inString"
-=edit in response to comment--
$mysqli = mysqli( host, user, password, db);
$inString = "(" . implode(", ", $_SESSION['preferences']) . ")";
$query = "SELECT * FROM ABC WHERE ID NOT IN $inString";
$res = $mysqli->query($query);
$obj = $mysqli->fetch_object();
Do you have all of these steps? The issue is that 'false'->fetch_object() is no good. 'false' is whats returned when a query has an error or has no results. This appears to be because your query is bad.
精彩评论