How to use variables in WHERE clause for a SQL SELECT query

大城市里の小女人 提交于 2020-02-25 05:26:08

问题


My SQL SELECT query has 3 variables set by a HTML form, however these variables can be null:

   $suburb = (isset($_POST['suburb'])) ? $_POST['suburb'] : '';
   $postcode = (isset($_POST['postcode'])) ? $_POST['postcode'] : '';
   $state = (isset($_POST['state'])) ? $_POST['state'] : '';

When all 3 variables are entered in the form the SELECT query processes fine (due to the AND's in the WHERE clause). But when one or more variables are blank, the SELECT query errors as it is looking for a NULL value in the databse.

What I want it to do is when a variable is null, I don't want a where clause for it

Query:

$sql = <<<SQL
SELECT 
    cs.customerRefId,
    cc.firstName,
    cc.lastName,
    cc.email,
    cc.phone,
    cc.mobile,
    cs.seekingAddressSuburb, 
    cs.seekingAddressPostcode, 
    cs.seekingAddressState
FROM 
    customer_seeking cs
LEFT JOIN
    customer_contact cc ON cc.customerRefId = cs.customerRefId
WHERE
cs.seekingAddressSuburb = '$suburb' AND
cs.seekingAddressPostcode = '$postcode' AND
cs.seekingAddressState = '$state'
SQL;  


if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}

For example if only the state variable is defined (cs.seekingAddressSuburb = '$suburb' AND cs.seekingAddressPostcode = '$postcode' AND are removed from the WHERE clause):

$sql = <<<SQL
SELECT 
    cs.customerRefId,
    cc.firstName,
    cc.lastName,
    cc.email,
    cc.phone,
    cc.mobile,
    cs.seekingAddressSuburb, 
    cs.seekingAddressPostcode, 
    cs.seekingAddressState
FROM 
    customer_seeking cs
LEFT JOIN
    customer_contact cc ON cc.customerRefId = cs.customerRefId
WHERE
cs.seekingAddressState = '$state'
SQL;  

回答1:


construct your query in a String then add one by one if each field have value. example :

$query = "select * from table1 where 1=1";
if(!empty($suburb)) $query.=" and cs.seekingAddressSuburb = '$suburb'";
if(!empty($postcode)) $query.=" and cs.seekingAddressPostcode = '$postcode'";
if(!empty($state)) $query.=" and cs.seekingAddressState = '$state'";
//run your query then



回答2:


Im not sure if this is what you want to do but you try this code in PHP FIDDLE

$a = 'a';
$b = 'b';
$c;

$SQL = "SELECT * FROM TABLE WHERE A = $a". (!empty($b) ? ' AND b ='. $b: '')  ." ". (!empty($c) ? 'AND C ='. $c: '');

echo $SQL;

TO RELATE IN YOUR CODE

$suburb = if(isset($_POST['suburb']));
$postcode = if(isset($_POST['postcode']));
$state = if(isset($_POST['state']));

$SQL = "SELECT cs.customerRefId,
cc.firstName,
cc.lastName,
cc.email,
cc.phone,
cc.mobile,
cs.seekingAddressSuburb, 
cs.seekingAddressPostcode, 
cs.seekingAddressState
FROM 
    customer_seeking cs
LEFT JOIN
    customer_contact cc ON cc.customerRefId = cs.customerRefId WHERE cs.seekingAddressSuburb = $suburb". (!empty($postcode) ? ' AND cs.seekingAddressPostcode ='. $postcode: '')  ." ". (!empty($state) ? 'AND cs.seekingAddressState ='. $state: '');

echo $SQL;


来源:https://stackoverflow.com/questions/17015766/how-to-use-variables-in-where-clause-for-a-sql-select-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!