0

This is my query

    $sql = "SELECT * FROM contact where isdeleted = 0  ";



          if ($language !="" && $language !="Empty" ){



                    $language_exp = explode(',', $language);
         $sql .= " INNER JOIN contactlanguage ON contact.id = contactlanguage.contactid
    INNER JOIN language ON contactlanguage.languageid = language.id where isdeleted = 0 AND language.language in ('".implode("', '", $language_exp)."') "; 

                }

 if ($contactgroup !="" && $contactgroup !="Empty" ){



    $contactgroup_exp = explode(',', $contactgroup);
    $sql .= " AND contactgroup in ('".implode("', '", $contactgroup_exp)."')";

    }

if i run the function the the query result like this

SELECT * FROM contact where isdeleted = 0 INNER JOIN contactlanguage ON contact.id = contactlanguage.contactid INNER JOIN language ON contactlanguage.languageid = language.id where isdeleted = 0 AND language.language in ('English', 'Arabic', '')

here isdeleted = 0 is twice can any one tell me how to remove isdeleted = 0 before the inner condition also before the query run,thanks

arok
  • 1,795
  • 7
  • 27
  • 56
Xavi
  • 2,552
  • 7
  • 40
  • 59
  • Uhm..select the string with your mouse and hit "Del"? It's an hardcoded string, just remove the part! – Damien Pirsy Jun 02 '14 at 08:02
  • i want to run the query when langauge is selected if its not selected it should run only SELECT * FROM contact where isdeleted = 0 – Xavi Jun 02 '14 at 08:03
  • You're likely also wonderfully open to [SQL Injection](http://security.stackexchange.com/a/25710/3396). You should be using [parameterized queries](http://stackoverflow.com/a/60496/812837). Now, you probably can't add an array -type parameter, but you _could_ add a list of parameters using this technique (and then fill it in). What table is `isDeleted` in anyways? – Clockwork-Muse Jun 02 '14 at 09:13

1 Answers1

1

The easy solution is:

$sql = "SELECT * FROM contact";
if ($language !="" && $language !="Empty" ){
    //...
    $sql .= " INNER JOIN .....";
}
else {
    $sql .= " where isdeleted = 0";
}

// run query...
$query = mysql_query($sql);
The Alpha
  • 143,660
  • 29
  • 287
  • 307