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

Possible Duplicate:
PHP & MYSQL: How can i neglect empty variables from select

hello all;

if i have 4 variables and i want to select DISTINCT values form data base

<?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'** ");

?>

note: some or all variables ($var1,$var2,$var3,$var4) can be empty

what i want: i want to neglect empty fields

lets say that $var1 (keywords) is empty it will select all empty fileds, but i want if $var1 is empty the result will be like

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

if $var2 is empty the result will be like

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

if $var1 and $var2 are empty the result will be like

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

and so on

this link contain some answers (but some of it not professional or not work) .. please it's urgent

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


Forgive me if anything is wrong, it's very late here and I just typed this in notepad on Windows, without an environment to test on. * Use with caution * :)

$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/58896.html

上一篇: 在symfony2中获取许多变量

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