MYSQL NOT IN php array [closed]

1

Hello, gentlemen

I'm working on a project for a hobby that I have and I'm having a hard time with the MYSQL part. I have an array that comes from the API and is decoded and inserts some data from the connections in the DB. So far, everything normal. However, the connections will be closed later - after all, clients will disconnect from the network. I need these records to be deleted from the DB, and then I thought about using MySQL's NOT IN. Searching the internet, I realized that I would have to use the php implode to be able to fit MYSQL format. So I created:

$comma_separated = "'" implode("", $csv[$i]) "'" ;
//Comando SQL - Remove do DB aeronaves que não partem do Brasil
$sql = "DELETE FROM connections WHERE callsign not in ('$comma_separated')";

Where, csv is the array with the data - $ i comes from a loop that fetches the API data. The problem is that by executing the code it simply deletes all records, including those that still have the callsign in the array.

How do I proceed in this situation? I have already researched in several places, including here in StackOverflow itself and found no reference.

From now on, thank you and would like to inform and apologize for any bizarre code. I am a beginner in language. For those who want to see some more of the code, it's up in GitHub by url: github.com/SirGwaihir/parsec

Embrace

    
asked by anonymous 25.07.2016 / 08:40

2 answers

1

Basically need single quotes and a comma:

$comma_separated = implode("','", $csv[$i]);

You also need to see if $csv[$i] is really what you want.

A demo code. Use echo to avoid testing in DB:

$lista = array( 'PY2UFO', 'PY2VB' );
$comma_separated = implode("','", $lista);
$sql = "DELETE FROM connections WHERE callsign not in ('$comma_separated')";
echo $sql;

See working at IDEONE .

    
25.07.2016 / 10:14
0

Looking superficially, one can see the lack of the delimiter.

implode("", $csv[$i])

Switch to

implode("', '", $csv[$i])

Tip

Before executing the query, check the integrity by doing a breakpoint:

shortly after this line

$sql = "DELETE FROM connections WHERE callsign not in ('$comma_separated')";

add

echo $sql; exit;

This is just to visualize how the query is being assembled.
If you are mounting correctly, disable the test or delete it.

//echo $sql; exit;

Note that the above tips may not solve the problem 100%. It is a solution tip based on the most likely hypothesis, as presented in the question.

    
25.07.2016 / 10:13