简体   繁体   English

PHP / MySQLi-遍历数组以构建动态WHERE子句

[英]PHP/MySQLi - looping through an array to build dynamic WHERE clause

I've been stuck on something for a couple of days now and I'm really hoping someone can help me please. 我已经坚持了几天了,我真的希望有人能帮我。

I have an array called $default_districts which generated by a query to find the postcode districts within a predefined radius from a point... so the number of values stored is dynamic. 我有一个名为$ default_districts的数组,该数组由查询生成,以在距点的预定义半径内找到邮政编码区...因此存储的值数量是动态的。

[0] => NR1
[1] => NR12
[2] => NR13
[3] => NR14
... etc.

Ultimately, I'm trying to find out how many customers are within a "radius" from another address. 最终,我试图找出在另一个地址的“半径”内有多少客户。 So, I want to use those values binded to a prepared statement... 所以,我想使用那些绑定到准备好的语句的值...

eg 例如

// get data records within the default radius
if ($stmt = $mysqli->prepare("SELECT clientdata_urn, clientdata_district
FROM tblhub_clientdata
WHERE clientdata_district = ?)) {

// bind param to vars
$stmt->bind_param( // LOOP THROUGH $default_districts HERE // ));

// execute statement
$stmt->execute();

// store
$stmt->store_result();

// get numrows
$total_numrows = $stmt->num_rows;

// close statement
$stmt->close();

} else {

// error
printf("Error counting total data within default radius: %s\n", $mysqli->error);

}

If I hardcode what I'm trying to create it would be something like this... 如果我对尝试创建的内容进行硬编码,它将是这样的……

SELECT clientdata_urn, clientdata_district
FROM tblhub_clientdata
WHERE clientdata_district = 'NR1' OR clientdata_district = 'NR12' OR clientdata_district = 'NR13' OR clientdata_district = 'NR14'

I'm a bit of a newbie and any help would be greatly appreciated. 我是一个新手,任何帮助将不胜感激。

Thank you. 谢谢。

Welcome to SO, @Rachi. 欢迎来到SO,@ Rachi。 I believe this will solve your problem. 我相信这会解决您的问题。

First, you're a lot better off using IN rather than a series of OR statements. 首先,使用IN比使用一系列OR语句要好得多。 I put together an SQLFIDDLE to demonstrate this. 我整理了一个SQLFIDDLE来演示这一点。

This takes the form 采取形式

SELECT COUNT(id) as num_users, clientdata_district
FROM tblhub_clientdata
WHERE clientdata_district IN ('NR12', 'NR14')
GROUP BY clientdata_district

The output gives you the number of users within each of the area codes in the array. 输出为您提供了阵列中每个区号内的用户数。 If you want the total number of users in ALL areas within your area (your question wasn't clear) then simply remove the GROUP BY line like this . 如果您想获得您所在区域内所有区域的用户总数(您的问题尚不清楚),则只需删除GROUP BY 这一行即可。

The next challenge is to build your string so that you can put it into the query. 下一个挑战是构建字符串,以便可以将其放入查询中。 The easiest way to do this is with the implode function . 最简单的方法是使用爆破功能

$query_string = implode(",", $default_districts)
$sql = "SELECT COUNT(id) as num_users, clientdata_district";
$sql.= "FROM tblhub_clientdata";
$sql.= "WHERE clientdata_district IN ($query_string)";
$sql.= "GROUP BY clientdata_district";

NOTE that this will not add ' marks to your area codes, but from what I've seen of your codes I don't think that will matter. 请注意,这不会在您的区号中添加'标记,但是根据我对您的区号的了解,我认为这无关紧要。 Worst case just throw away the implode statement and build the string in a FOR loop. 最坏的情况就是丢掉implode语句,并在FOR循环中构建字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM