返回SQL / PHP中one =列的所有值

时间:2014-12-05 13:18:03

标签: php mysql sql

我有以下SQL查询:

$workspace = $_GET['workspace']; 
$location = $_GET['location']; 

$query="SELECT * FROM `offices` WHERE (`workspace` LIKE '%".$workspace."%') AND (`county` = '$location') AND (`active` = 'yes') LIMIT 0, 30 ";
$result=mysql_query($query);

$ location值从上一页的表单返回,该表单包含32个县的列表,编号为1-32。我想有一个选项来选择"所有县"这将返回与查询和任何县中的其他条件匹配的结果。县列表存储为SQL表中一列中1-32的数字。

如果最近被问到但是无法找到任何内容的道歉。谢谢!

3 个答案:

答案 0 :(得分:1)

$workspace = mysql_real_escape_string( $_GET['workspace'] ); 

$location = mysql_real_escape_string( $_GET['location'] ); 

$query="SELECT * FROM `offices`";

$conditions = array( "`active` = 'yes'" );

if( !empty( $workspace ) )
{
$conditions []= "`workspace` LIKE '%".$workspace."%'"
}

if( !empty( $location ) )
{
$conditions []= "(`county` = '$location')";
}

if( !empty( $conditions ) )
{
$query .= " WHERE " . implode( ' AND ', $conditions );
}

$query .= "LIMIT 0, 30 ";

$result=mysql_query($query);

它有点粗糙,准备就绪,但应该这样做

在您的国家/地区列表中,只需输入:

<select name="location">
<option id="0">All Countries</option>
<option id="1">...</option>
</select>

答案 1 :(得分:0)

我没有抓住你的问题。但以下内容将简单介绍该怎么做:

$locationCondition = "(`county` = '$location') AND ";
if ($location == "ALL") 
    $locationCondition = "";
$query="SELECT * FROM `offices` WHERE (`workspace` LIKE '%".$workspace."%') AND ".$locationCondition."(`active` = 'yes') LIMIT 0, 30 ";

答案 2 :(得分:0)

我希望这有助于我思考

$query="SELECT * FROM `offices` WHERE (`workspace` LIKE '%".$workspace."%') AND (`county`    in ('$location')) AND (`active` = 'yes') LIMIT 0, 30 ";