【问题标题】:How to create a SQL query in PHP with differents variables如何在 PHP 中使用不同的变量创建 SQL 查询
【发布时间】:2014-06-03 16:37:33
【问题描述】:

当我创建一个依赖于用户在不同复选框中选择的许多变量的 sql 查询时,我遇到了一个问题。 我做了一个 httprequest GET 和他们,当我提议创建查询时,我检查变量并逐渐创建查询。我给你看PHP代码:

$link =  mysql_connect($hostname, $username, $password) or die('No se pudo conectar: '  . mysql_error());
//echo 'Conectado satisfactoriamente';
mysql_select_db('Agenda Juvenil') or die('No se pudo seleccionar la base de datos');
mysql_query('SET CHARACTER SET utf8');

$query="SELECT id, title, barrio_smultiple, coordenadas_p_0_coordinate,     coordenadas_p_1_coordinate, gratuita_b FROM eventosDiarios WHERE";

// check for post data

if (isset($_GET['franjas0'])){
 $franja0 = $_GET['franjas0'];
 $query.="franja_smultiple IN ('$franja0'";
 }

if (isset($_GET['franjas1'])){
 $franja1 = $_GET['franjas1'];
 $query.=",'$franja1'";
 }

if (isset($_GET['franjas2'])){
 $franja2 = $_GET['franjas2'];
 $query.=",'$franja2'";
 }

$query.=")";

// get a product from products table
$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());

if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["eventos"] = array();

while ($row = mysql_fetch_array($result)) {
    // temp user array
    $evento = array();
    $evento["id"] = $row["id"];
    $evento["title"] = $row["title"];
    $evento["barrio_smultiple"] = $row["barrio_smultiple"];

    $evento["coordenadas_p_0_coordinate"] = $row["coordenadas_p_0_coordinate"];
    $evento["coordenadas_p_1_coordinate"] = $row["coordenadas_p_1_coordinate"];
    $evento["gratuita_b"] = $row["gratuita_b"];


    // push single product into final response array
    array_push($response["eventos"], $evento);
}
// success
$response["success"] = 1;

// echoing JSON response
echo json_encode($response);

} else {
// no products found
$response["success"] = 0;
$response["message"] = "No se han encontrado eventos";

// echo no users JSON
echo json_encode($response);
}

查询必须返回一些东西,但我什么都没有。变量通过t发送

List<NameValuePair> params = new ArrayList<NameValuePair>();

我使用的是安卓系统。 有人可以帮帮我吗??

提前致谢。

问候。

【问题讨论】:

  • 在哪里???您的查询不完整
  • @mona 因为 OP 正在附加到变量,也许?
  • @vld 是的,但是如果 First if 条件失败怎么办
  • @mona 我们实际上不知道它是否会因此而失败。我建议 OP 提供更多代码,以便我们可以说一些更具体的东西。就目前而言,OP 说查询没有返回任何内容,所以我假设生成的查询是有效的,但实际的条件逻辑不是,因此没有返回任何内容。
  • @Vld 我首先确定如果没有失败,因为我总是点击这个选项。问题是查询没有返回任何内容。是创建这样的查询的好方法吗?

标签: php android mysql get


【解决方案1】:

您的代码需要在 WHERE 之后有一个空格。不得不说

...b FROM eventosDiarios WHERE ";

而不是

....b FROM eventosDiarios WHERE";

否则你会得到一个格式错误的查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    相关资源
    最近更新 更多