Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to create a dynamically generated mySQL select statement using PHP
    text
    copied!<p>I have 5 variables which can either be true or false and I have to generate different sql SELECT statements for each different possible outcome. Right now I have a ton of if else statements but I'm wondering if theres a smarter way to do this?</p> <p>so for example, i have </p> <pre><code>if (x=true AND y=false AND z=false AND a=false AND b =false) { $sql= "SELECT...." } else if(x=true AND y=true AND z=false AND a=false AND b=false) { $sql= "SELECT...." } </code></pre> <p>The first select statement is if the user entered text and didnt select anything else:</p> <pre><code>$sql="SELECT CompanyName, Keywords, Product, Industry, Link, region, hot FROM searchtest_tbl WHERE Keywords LIKE '%$formSearch%' OR CompanyName LIKE '%$formSearch%' OR Product LIKE '%$formSearch%' OR Industry LIKE '%$formSearch%' ORDER BY hot DESC, CompanyName"; </code></pre> <p>this statement is if they only selected from the Industry dropdown:</p> <pre><code>$sql="SELECT CompanyName, Product, Industry, Link, hot, region FROM searchtest_tbl WHERE Industry='$formIndustry' ORDER BY hot DESC, CompanyName"; </code></pre> <p>and here is one if they entered text, selected an industry, but didnt select anythign else:</p> <pre><code>$sql="SELECT CompanyName, Product, Industry, Link, hot, region FROM searchtest_tbl WHERE Industry='$formIndustry' AND (Keywords LIKE '%$formSearch%' OR CompanyName LIKE '%$formSearch%' OR Product LIKE '%$formSearch%') ORDER BY hot DESC, CompanyName"; </code></pre> <p>The rest are basically like this, but if the other fields are selected it will say <code>WHERE Product='$formProduct' AND</code>.... etc</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload