How to mount a query with optional conditions?

Asked

Viewed 296 times

3

In the example:

SELECT * FROM exemplo WHERE ID in(2, 3, 4) 

He brings if he has these ID at the bank.

How to mount a query which places this condition only if the array in PHP have elements?

PHP:

$id= implode(', ', $_GET['id']);
$nome= implode(', ', $_GET['nome']);
$cidade= implode(', ', $_GET['cidade']);

$query = mysql_query("SELECT * FROM EXEMPLO WHERE id IN($id) AND nome IN($nome) AND cidade IN($cidade)");

//Se tiver de acordo com a condição traz a linha dentro da condição, se não traz todas as linhas.

I tried it like this, but I don’t know if it’s right;

$id= "(id='".implode("' OR id='", $_GET['id'])."') AND";
$nome= "(nome='".implode("' OR nome='", $_GET['nome'])."') AND";
$cidade= "(cidade='".implode("' OR cidade='", $_GET['cidade'])."')";

$query = mysql_query("SELECT * FROM EXEMPLO WHERE $id $nome $cidade");
  • 1

    The time you finish changing the problem, you warn me and I try to answer correctly. Every time I hit the answer you change something.

  • The ")in the last row.

  • Yes I forgot to type here rs..

  • Face this implode, it will only work if you have an array with the Ids in that $_GET['id']; type $get_data = array(1,2,3,4,5,6); implode(', ', $get_data);

  • Yes I use various inputs with name="id[]", this causes me to send via get an array.

1 answer

4


Better resolve in PHP and analyze if the array is empty or not. If it is empty, bring everything, if it is not, use the condition. After the various changes in formulation, according to the comments I would do something like this:

$query = "SELECT * FROM exemplo WHERE TRUE";
if (!empty($condicao1)) {
    $lista = implode(",", $condicao1);
    $query .= " AND ID NOT IN ($lista)";
}
if (!empty($condicao2)) {
    $lista = implode(",", $condicao2);
    $query .= " AND NOME NOT IN ($lista)";
}
if (!empty($condicao3)) {
    $lista = implode(",", $condicao3);
    $query .= " AND CIDADE NOT IN ($lista)";
}

I put in the Github for future reference.

Placed echo Just to illustrate, use in the PDO as you see fit. Of course if I had the code, I knew the real situation, I could help more, but abstractly this is it. In concrete situation I would sophisticate this.

This technique can be used to mount any type of condition.

  • But then he won’t bring these ids.

  • Ah, you want him to look for those, if he doesn’t find anything, then he should bring it all, is that it? If that’s it, I think with just one SELECT can not, I need to think. As it is I will erase and if time to find a solution, I put. I even thought of a simple solution with SELECT but she’s dirty, I’d only wear it if I didn’t find a better one.

  • So, I get an array with multiple ids, then I use the implode to separate, and use it in an IN, but I want when the array is empty, bring all the results without the filter.

  • Okay, thank you ^^

  • It would have been better to have put your PHP code that generates this array to get a better sense of what you’re doing. I think in this case it’s best to solve it by PHP.

  • I’m not going to use just one IN, if I didn’t use php’s if to separate, it was going to look something like this: SELECT * FROM example WHERE ID IN($codicao1) AND name IN($codicao2) AND city IN($codicao3)

  • Well, your question has completely changed from what you originally wrote, right? But it would still do this way, it would only be one if more complex, would have to build the string.

  • Ready edited the question see there if you understand better!

  • I used it differently I saw there edited, only see if you think it right! For in that way he can receive more than one city too, in that if he only receives one!

  • It is not correct, nor does it perform. Nor did I mention the security flaw in this code but this is another matter.

  • I’ve tried that and sure enough, yes, the flaws I’ve dealt with by taking out all the letters and accents with the e and ereg_raplace. I was just wondering if you had anything simpler to use.

  • If you say it worked, go for it. It does in certain circumstances, if you can guarantee that they will always be present, great. If you can’t, you’ll be the one in trouble.

  • So by the tests I did worked, it does not pass letter and nor accents only numbers that would be ids. Let’s see when the user will use, because it is the only one that can actually do a full rsrsrs test.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.