0

我试过这个:

$result = mysql_query("SELECT * from business WHERE ptype = '".$_GET["ptype"]."' AND state = '".$_GET["state"]."' AND city = '".$_GET["city"]."'") or die(mysql_error());   

但如果我不放 3 个 GET,我会从 MySql 收到一个错误:

localhost/get_allinfo.php?ptype=PUB&state=Center

然后我得到一个 MySQL 错误

我试过这个:
但是如果我把“状态”放在 URL 中并不重要$_GET["ptype"]) & (isset($_GET["state"]))

我只从“ptype”得到结果

    if (isset($_GET["ptype"])) {
    $ptype = $_GET["ptype"];
    $result = mysql_query("SELECT * from business WHERE ptype = '$ptype'") or die(mysql_error());

}elseif(isset($_GET["ptype"]) & (isset($_GET["state"]))){
    $ptype = $_GET["ptype"];
    $state = $_GET["state"];
    $result = mysql_query("SELECT * from business WHERE ptype = '$ptype' and WHERE state = '$state") or die(mysql_error());
}else {

    $result = mysql_query("SELECT * FROM `business`") or die(mysql_error());
}
4

2 回答 2

0

以下是错误的

SELECT * from business WHERE ptype = '$ptype' and WHERE state = '$state

它应该是:

SELECT * from business WHERE ptype = '$ptype' AND state = '$state'

在 WHERE 上查找文档并在 $state 中添加结束引号

还可以更改 if 条件的位置

if(isset($_GET["ptype"]) & (isset($_GET["state"]))){
$ptype = $_GET["ptype"];
$state = $_GET["state"];
$result = mysql_query("SELECT * from business WHERE ptype = '$ptype' AND state = '$state'") or die(mysql_error());
}elseif (isset($_GET["ptype"])) {
$ptype = $_GET["ptype"];
$result = mysql_query("SELECT * from business WHERE ptype = '$ptype'") or die(mysql_error());

}if(isset($_GET["ptype"]) & (isset($_GET["state"]))){
$ptype = $_GET["ptype"];
$state = $_GET["state"];
$result = mysql_query("SELECT * from business WHERE ptype = '$ptype' and WHERE state = '$state") or die(mysql_error());
}else {

$result = mysql_query("SELECT * FROM `business`") or die(mysql_error());
}

您的 ptype 始终设置,这意味着您的控件始终位于第一个 if 语句中。即使设置了状态。

PS。了解 PDO/mysqli,你不会后悔的。

于 2012-10-14T14:23:35.403 回答
0

这应该使它更健壮一点:

$sql = 'SELECT * from business';
$arrQueryString = array();
if(isset($_GET['ptype'])) $arrQueryString[] = 'ptype="'.$_GET['ptype'].'"';
if(isset($_GET['state'])) $arrQueryString[] = 'state="'.$_GET['state'].'"';
if(isset($_GET['city'])) $arrQueryString[] = 'city="'.$_GET['city'].'"';
$sql .= (count($arrQueryString)) ? ' WHERE '.implode(' AND ',$arrQueryString) : '';
$result = mysql_query($sql) or die(mysql_error());
于 2012-10-14T14:33:54.690 回答