Home:ALL Converter>PHP & MYSQL: How can i neglect empty variables from select

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

Ask Time:2011-03-05T06:45:59         Author:cash-cash

Json Formatter

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

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

?>

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

Author:cash-cash,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/5200121/php-mysql-how-can-i-neglect-empty-variables-from-select
cash-cash :

Thanks alot every one specially experimentX .. Your answer helped me to get the right function i Just replaced (isset) with (!empty) .. Then every thing will be more than OK\n\n$vars = array(\n (!empty($_GET[\"var1\"]))? \" keyword = '\". $_GET[\"var1\"] .\"' \": null, \n (!empty($_GET[\"var2\"]))? \" author = '\". $_GET[\"var2\"] .\"' \": null,\n (!empty($_GET[\"var3\"]))? \" date = '\". $_GET[\"var3\"] .\"' \": null,\n (!empty($_GET[\"var4\"]))? \" forums = '\". $_GET[\"var4\"] .\"' \": null\n );\n\n\nfunction myfilterarray($var)\n{\n return !empty($var)?$var: null;\n}\n\n$newvars = array_filter($vars, 'myfilterarray');\n\n$where = join(\" OR \", $newvars);\n\n$sql = \"SELECT DISTINCT title, description FROM table \".(($where)?\"WHERE \".$where: null);\n\necho $sql;\n\n\nwith this function if there is empty variable it will be neglected\n\nThanks again every one for your helpful suggestion",
2011-03-05T10:57:12
Matt :

make your select statement string before you call mysql_query(...) so do something along the lines of this:\n\n$queryString = \"Select DISTINCT title, description FROM table WHERE\";\nif(!empty($var1))\n $queryString .= \" keywords = $var1\";\n\n\nand so forth for all of your variables. you could also implement a for loop and loop through your $var1 - $var# and check for !empty($var#)",
2011-03-04T22:52:31
Jerome :

Why do you not simply build a if else structure?\nLike\n\nif ($var1!=\"\" && $var2!=\"\" && $var3!=\"\" && $var4!=\"\"){\n $result = mysql_query(\"SELECT DISTINCT title,description FROM table WHERE keywords ='$var1' OR author='$var2' OR date='$var3' OR forums='$var4' \")\n} else if ($var2!=\"\" && $var3!=\"\" && $var4!=\"\"){\n $result = mysql_query(\"SELECT DISTINCT title,description FROM table WHERE author='$var2' OR date='$var3' OR forums='$var4' \");\n} else if {\n...\n}\n",
2011-03-04T22:52:54
Philoxopher :

(I just posted the below in his duplicate post, so I'm re-posting the below here)\n\nForgive 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 * :)\n\n$vars = array(\n'blah1' => '',\n'blah2' => '',\n'blah3' => '',\n);\n\n $sql_statement = \"SELECT first, last FROM names WHERE\";\n\n $clause = \"\";\n foreach($vars as $k=$v)\n{\n$k = trim($k);\nif(!empty($k))\n{\n $clause .= \" `$k` = '$v' OR\";\n}\n}\n$clause = rtrim($clause, \"OR\");\n\n// $clause should have what you want.\n",
2011-03-05T08:11:33
Dimitry :

Try this.\n\n\n$vars = array(\n 'keywords' => '', // instead of var1\n 'author' => '', // instead of var2\n 'date' => '', // instead of var3\n 'forums' => '', // instead of var4\n);\n\n$where = array();\nforeach ($vars as $varname => $varvalue) {\n if (trim($varvalue) != '') $where[] = \"`$varname` = '\" . mysql_real_escape_string($varvalue) . \"'\";\n}\n$result = mysql_query(\"SELECT DISTINCT title, description FROM table WHERE \" . join(\" OR \", $where));\n",
2011-03-04T22:49:29
yy