$ _GET的四个变量不能与MYSQL一起使用

时间:2012-10-14 14:15:10

标签: php android mysql

我试过了:

$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中收到错误:

  

本地主机/ get_allinfo.php p-型= PUB&安培;状态=中心

然后我收到MySQL错误

我试过这个:
但是如果我在网址$_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());
}

2 个答案:

答案 0 :(得分: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始终设置,这意味着您的控件始终位于1st if语句中。即使国家已经确定。

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

答案 1 :(得分: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());