PHP& MYSQL:我如何忽略select中的空变量

时间:2011-03-04 22:45:59

标签: php mysql

如果我有4个变量,我想从数据库中选择DISTINCT值

<?php
$var1 = ""; //this variable can be blank
$var2 = ""; //this variable can be blank
$var3 = ""; //this variable can be blank
$var4 = ""; //this variable can be blank

$result = mysql_query("SELECT DISTINCT title,description FROM table WHERE **keywords ='$var1' OR author='$var2' OR date='$var3' OR forums='$var4'** ");

?>

注意:部分或全部变量($ var1,$ var2,$ var3,$ var4)可以为空

我想要的: 我想忽略空场

假设$ var1(关键字)为空它会选择所有空文件,但我想如果$ var1为空,结果就像

$result = mysql_query("SELECT DISTINCT title,description FROM table WHERE author='$var2' OR date='$var3' OR forums='$var4' ");

如果$ var2为空,结果将类似于

$result = mysql_query("SELECT DISTINCT title,description FROM table WHERE keywords ='$var1' OR date='$var3' OR forums='$var4' ");

如果$ var1和$ var2为空,结果将类似于

$result = mysql_query("SELECT DISTINCT title,description FROM table WHERE date='$var3' OR forums='$var4' ");

等等

6 个答案:

答案 0 :(得分:4)

试试这个。


$vars = array(
  'keywords' => '', // instead of var1
  'author' => '',  // instead of var2
  'date' => '', // instead of var3
  'forums' => '', // instead of var4
);

$where = array();
foreach ($vars as $varname => $varvalue) {
  if (trim($varvalue) != '') $where[] = "`$varname` = '" . mysql_real_escape_string($varvalue) . "'";
}
$result = mysql_query("SELECT DISTINCT title, description FROM table WHERE " . join(" OR ", $where));

答案 1 :(得分:1)

非常感谢每一个特别 experimentX ..你的回答帮助我获得了正确的功能我刚用(!空)替换(isset)..那么每件事都会超过OK

$vars = array(
        (!empty($_GET["var1"]))? " keyword = '". $_GET["var1"] ."' ": null, 
        (!empty($_GET["var2"]))? " author  = '". $_GET["var2"] ."' ": null,
        (!empty($_GET["var3"]))? " date    = '". $_GET["var3"] ."' ": null,
        (!empty($_GET["var4"]))? " forums  = '". $_GET["var4"] ."' ": null
    );


function myfilterarray($var)
{
    return !empty($var)?$var: null;
}

$newvars = array_filter($vars, 'myfilterarray');

$where = join(" OR ", $newvars);

$sql = "SELECT DISTINCT title, description FROM table ".(($where)?"WHERE ".$where: null);

echo $sql;

有了这个函数,如果有空变量,它将被忽略

再次感谢每一位有用的建议

答案 2 :(得分:0)

在调用mysql_query(...)之前创建select语句字符串,以便按照以下方式执行:

$queryString = "Select DISTINCT title, description FROM table WHERE";
if(!empty($var1))
   $queryString .= " keywords = $var1";
所有变量的

等等。你也可以实现一个for循环并循环遍历$ var1 - $ var#并检查!empty($ var#)

答案 3 :(得分:0)

为什么不简单地构建一个if else结构? 像

if ($var1!="" && $var2!="" && $var3!="" && $var4!=""){
   $result = mysql_query("SELECT DISTINCT title,description FROM table WHERE keywords ='$var1' OR author='$var2' OR date='$var3' OR forums='$var4' ")
} else if ($var2!="" && $var3!="" && $var4!=""){
   $result = mysql_query("SELECT DISTINCT title,description FROM table WHERE author='$var2' OR date='$var3' OR forums='$var4' ");
} else if {
...
}

答案 4 :(得分:0)

(我刚刚在他的重复帖子中发布了以下内容,所以我在这里重新发布了以下内容)

请原谅我,如果出现任何问题,这里已经很晚了,我只是在Windows上的记事本中键入了这个,没有可以测试的环境。 *谨慎使用*:)

$vars = array(
'blah1' => '',
'blah2' => '',
'blah3' => '',
);

 $sql_statement = "SELECT first, last FROM names WHERE";

 $clause = "";
 foreach($vars as $k=$v)
{
$k = trim($k);
if(!empty($k))
{
    $clause .= " `$k` = '$v' OR";
}
}
$clause = rtrim($clause, "OR");

// $clause should have what you want.

答案 5 :(得分:0)

嗯,有manu方法这样做,但我找到的最短路径是创建以下形式的数组

$vars = array(
        (isset($_GET["var1"]))? " keyword = '". $_GET["var1"] ."' ": null, 
        (isset($_GET["var2"]))? " author  = '". $_GET["var2"] ."' ": null,
        (isset($_GET["var3"]))? " date    = '". $_GET["var3"] ."' ": null,
        (isset($_GET["var4"]))? " forums  = '". $_GET["var4"] ."' ": null
    );

function myfilterarray($var)
{
    return !empty($var)?$var: null;
}

$newvars = array_filter($vars, 'myfilterarray');

$where = join(" OR ", $newvars);

$sql = "SELECT DISTINCT title, description FROM table ".(($where)?"WHERE ".$where: null);

echo $sql;

http://localhost/?var1=sadfsadf&var2=sadfasdf&var3=asdfasdf

的结果
 SELECT DISTINCT title, description FROM table WHERE keyword = 
 'sadfsadf' OR author = 'sadfasdf' OR date = 'asdfasdf' 

http://localhost/?

的结果
SELECT DISTINCT title, description FROM table