Jump to content

exclude values in an array from mysql select statement????


Recommended Posts

Beginner here!!!

 

I am trying to exclude the values in an array from being searched for in a mysql select statement

 

WHERE P.id != '$new_array' AND .....ect;

 

Doesn't work, searches for all P.id's normally and doesn't exclude the id's in the array.

 

But

 

If I use foreach where the array comes from, only the last item in the array is acted upon.

 

print_r shows the array normally before doing the foreach statement

 

but after creating a foreach statement:

 

if(count($array) > 0){
foreach($array as $item);
  $new_array = $item;
}

 

Now

list_r  $new_array  displays the last value only and not as an array but just the string by itself... 428

 

The id 428 is excluded from the select process but not the other values in the array.

 

I know I can change it to $new_array = array($item)

print_r shows new_array normally but I'm back to the select statement not excluding anything.

 

What's up!!!!

Thanks,

What I need is for the values in the array to be excluded at the mysql select statement, just like in my example if possible.

SELECT ... FROM.... WHERE ..... AND P.id != '$new_array'

 

BTW, I tried implode to make a comma separated list and a space separated list, didn't work

 

if(count($array) > 0){
foreach($array as $item){
  $new_array = implode(",",array);
}
}

the only problem is the last ","

solution:

<?php
if(count($array) > 0){
$last = array_pop($array);
foreach($array as $item){
  	if ($item != $last){
      $exclude_list .= "'".$item."', ";
  }
}
$exclude_list .= "'".$last."'";
}
$sql = "SELECT * FROM `table_name WHERE P.id NOT IN ({$exclude_list})";

Thanks everyone...

 

sasa

$new_array = implode("','", $array);

 

it works

echo $new_array

427','428

 

I tried it with P.id != '$new_array' and that works as well as with an empty array.

 

I also tried it with  implode("''", $array);

it worked also

echo $new_array

427'428

 

jonsjava

$sql = "SELECT * FROM `table_name WHERE P.id NOT IN ({$exclude_list})";

 

If the array is empty it doesn't work. " NOT IN ({$exclude_list}) "

It kills the select statement and nothing gets selected if the array (or list, whichever it is) is empty.

 

I tried it with P.id NOT IN ('$exclude_list') and it works

I tried it with P.id != '$exclude_list' and it works

 

both of the above yielded:

 

echo $exclude_list

'427', '428'

 

-----------------------

It seem that we have 3 or 4 ways to yield the same result. I will leave this as unsolved for a while if anyone would care to comment on which method is or isn't correct.

 

Thank again, Lance

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.