gpt4 book ai didi

php - 如何在 PHP 中使用不同变量创建 SQL 查询

转载 作者:行者123 更新时间:2023-11-30 00:11:21 24 4
gpt4 key购买 nike

当我创建一个 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);
}

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

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

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

提前致谢。

问候。

最佳答案

您的代码在 WHERE 后面需要有一个空格。需要说的是

...b FROM eventosDiarios WHERE ";

而不是

....b FROM eventosDiarios WHERE";

否则您将收到格式不正确的查询。

关于php - 如何在 PHP 中使用不同变量创建 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24020466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com