PHP和MYSQL:我如何忽略来自select的空变量

可能重复:
PHP和MYSQL:我如何忽略来自select的空变量

大家好;

如果我有4个变量,我想选择DISTINCT值形式的数据库

<?php
$var1 = $_GET["var1"]; //this variable can be blank
$var2 = $_GET["var2"]; //this variable can be blank
$var3 = $_GET["var3"]; //this variable can be blank
$var4 = $_GET["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' ");

等等

这个链接包含一些答案(但其中一些不是专业或不工作)..请它是紧急的

PHP和MYSQL:我如何忽略来自select的空变量


如果有什么不对,请原谅我,这里已经很晚了,我只是在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.
链接地址: http://www.djcxy.com/p/58895.html

上一篇: PHP & MYSQL: How can i neglect empty variables from select

下一篇: Command not found error in Bash variable assignment