【问题标题】:$_GET variable Array with MySQL Query$_GET 带有 MySQL 查询的变量数组
【发布时间】:2012-12-18 05:41:33
【问题描述】:

我无法弄清楚如何在包含 MySQL 查询的 PHP 中使用 if 语句。这是我目前所拥有的(尚未完成):

$industry = $_GET['industry'];
$location = $_GET['location'];
$position = $_GET['position'];
$region = $_GET['region'];

if($industry=="any"){

}

if($location=="any"){

}

if($position=="any"){

}

if($region=="any"){

}

$sql_cat = "
SELECT *
FROM jobs
WHERE industry_cat='$industry' && location_cat='$location' && position_cat='$position' && region_cat='$region'
ORDER BY id DESC
";
$result_cat = $mysqli->query($sql_cat);
$num_rows_cat = mysqli_num_rows($result_cat);

基本上,我想说的是,如果任何变量设置为“任何”,那么我希望这些变量代表所有可能性(每个变量的表格中有五个可能性)。我想我会在 where 子句中使用数组(例如 WHERE Industry IN ($industry)),但如果有人真的选择了一个变量,它就行不通了。

我可能有点偏离了轨道,但我很难把它包起来。任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 您能否提供更多您真正想要的信息
  • 你的变量是什么(你说的五种可能性)?
  • 这只是人们提交的一个搜索表单,并且有一个“任何”选项,当我查询我的数据库时,我想代表该变量的所有可能性。这有意义吗?
  • @Sithu,为此,我们可以只使用“661”“662”“663”和“664”作为可以选择的变量。

标签: php mysql get


【解决方案1】:

如果我没有误解你的问题,我会这样做。

$sql = "SELECT * FROM jobs WHERE 1";
if($industry !== "any"){
    $sql .= " AND industry_cat = '$industry'"
}
if($location !== "any"){
    $sql .= " AND location_cat = '$location'"
}
if($position !== "any"){
    $sql .= " AND position_cat = '$position'"
}
if($region !== "any"){
    $sql .= " AND region_cat = '$region'"
}
$sql .= " ORDER BY id DESC"
$result = $mysqli->query($sql);

如果您确定它们是整数,您可以删除查询中变量周围的单引号。

【讨论】:

  • 现在,假设某人实际上在其中一个上选择了“任何”以外的东西,它是否还会显示该变量?
  • @MxmastaMills,例如,如果为行业选择 661,则第一个条件将通过在查询中添加条件 AND industry_cat = '661' 来解决此问题。请注意,您可以使用!= 代替!==。后者是精确匹配,包括数据类型。你可以试试:)
  • I am receiving an error: "Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /example/example.php on line 57" when the search results are chosen and no results come向上。它正确显示了内容(行数为 0 等),但它也在页面上给了我错误消息。我正在努力思考,但无法弄清楚为什么结果会是空的......
  • @MxmastaMills,这是因为没有给mysqli_num_rows()正确的mysqli结果。可能,查询失败。检查您的页面中的查询。我建议使用if 包装您的查询执行程序,例如if($result = mysqli_query($sql)){ $count = mysqli_num_rows($result); }else{ echo mysqli_error($result); ,即如果$sql 失败,它不会给出这样的错误。另请注意,您使用的是 mysqli 对象还是 mysqli 函数?即$mysqli->query($sql)mysqli_query($sql);
  • 啊,你是对的。上面的代码中有一个小错误(iregion_cat 而不是 region_cat),我之前没有注意到。再次感谢您的帮助,它为我节省了很多时间,实际上也教会了我很多东西!
【解决方案2】:

逐步构建你的 where 子句

$industry = $_GET['industry'];
$location = $_GET['location'];
$position = $_GET['position'];
$region = $_GET['region'];

if($industry!="any"){
    $where[] = 'industry_cat=?';
    $where_args[] = $industry;
}

if($location!="any"){
    $where[] = 'location_cat=?';
    $where_args[] = $location;
}

if($position!="any"){
    $where[] = 'position_cat=?';
    $where_args[] = $position;
}

if($region!="any"){
    $where[] = 'region_cat=?';
    $where_args[] = $region;
}

$where_clause = implode(' and ', $where);

$sql_cat = "
SELECT *
FROM jobs
where $whereclause
ORDER BY id DESC
";
$stmt = $mysqli->prepare($sql_cat);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多