Php 带2个变量的循环SQL查询

Php 带2个变量的循环SQL查询,php,mysql,loops,Php,Mysql,Loops,我有一个带有3个字段的表(标签),activityID、placeID、tagID(第4个,ID为PKey)。我想用两个数组搜索这个表,一个是位置,一个是标记。对于每个匹配,我都要返回activityID。然后我想在另一个表(activity)中使用这个activityID列表,每个表都具有相同的PlaceID数组。我开始把它作为循环放在一起,但我看到很多东西都说不要这样做。我想我需要使用临时表,但这也可能不是必需的。无论如何,我也在努力用循环来完成它,所以我想我应该发布一个大概的想法,看看是否

我有一个带有3个字段的表(标签),activityID、placeID、tagID(第4个,ID为PKey)。我想用两个数组搜索这个表,一个是位置,一个是标记。对于每个匹配,我都要返回activityID。然后我想在另一个表(activity)中使用这个activityID列表,每个表都具有相同的PlaceID数组。我开始把它作为循环放在一起,但我看到很多东西都说不要这样做。我想我需要使用临时表,但这也可能不是必需的。无论如何,我也在努力用循环来完成它,所以我想我应该发布一个大概的想法,看看是否有人能给我指出正确的方向,而不是努力去做一些不好的练习。。。此代码不起作用,但显示了总体思路..编辑。。。我只想在第一部分中解决这个循环,第二部分我需要作为一个循环离开

$places = array("London","Madrid","Paris","Rome"); 
$tags = array("Shopping","Sight","Bar","Club");
$num_places = count($places);
$num_tags = count($tags);

/* I want to remove the loop from this section */
$counterP = 0; 
while($counterP <= ($num_places)) {
  $counterT = 0; 
  while($counterT <= ($num_tags)) {
    $conn->query('INSERT INTO temp (activityID)
    SELECT activityID, placeID
    FROM tags
    WHERE placeID = "'.$place[$counterP].'" AND tagID = "'.$tag[$counterT].'"');
  $counterT++;
  }
$counterP++;
}

/* This section will stay in a loop */
$counterP = 0; 
while($counterP <= ($num_places)) {
$sql_interests = 'SELECT a.summary, a.image, a.link
  FROM activity a 
  LEFT JOIN temp t
  ON t.activityID = a.activityID 
  WHERE a.placeID = "'.$place[$counterP].'"';

  $interests = array();
  $interests_result = $conn->query($sql_interests);
  if ( !empty($interests_result)) {
    while($interests_row = $interests_result->fetch_assoc()) {
      $interests[] = array($interests_row["summary"],$intersts_row["image"],$interests_row["link"]);
    }
    /* do stuff with data */
  }
  $counterP++;
}
$places=array(“伦敦”、“马德里”、“巴黎”、“罗马”);
$tags=数组(“购物”、“观光”、“酒吧”、“俱乐部”);
$num_places=计数($places);
$num_tags=计数($tags);
/*我想删除此部分中的循环*/
$counter=0;
而($counter fetch_assoc()){
$interests[]=数组($interests_行[“summary”],$interests_行[“image”],$interests_行[“link”]);
}
/*处理数据*/
}
$counter++;
}

由于我目前只能使用手机,我只能建议您做以下事情:

  • 以“,”作为分隔符连接两个数组
  • 使用IN()函数编写where子句。比如:placeID在(‘伦敦’、‘马德里’、‘巴黎’、‘罗马’)和tagID在(…)

  • 这将给出您所需的结果。

    mysql-only方法。where子句过滤带有in子句的标记,join将使您进入活动表。
    a
    t
    只是为了便于阅读或懒惰(像我一样)而使用的别名


    你能摆弄2张或3张桌子吗?有人只会在mysql中把它搞定,因为1个选项将在一分钟内完成……然后您需要指定“匹配”的含义。那么,是伦敦是一场比赛,还是(伦敦和购物)或()。。。等等。括号中有matterOK,所以这里有一个SQLFIDLE,每个表中有一些项目。所以我有一个位置数组和标签数组。我想找到我的标签表中的所有组合,并获得每个匹配的关联活动。然后,我在一个新的查询中使用匹配的活动列表,从“活动”表中获取所有相关数据。仅供参考,我需要在第二部分中使用循环,因为我将通过placeID在部分中构建HTML。这主要是第一部分…假设我有3个位置和3个标签。您对结果的预期排列计数是多少?我可以写“WHERE placeID IN$places”吗?谢谢@Drew(再次:-)!一个关键问题。。。。我是否可以使用数组使其
    在$tags中的t.tagID
    如果在PHP中创建这样一个带有连接的$sqlString,那么是的。我不是说用准备好的语句进行PHP绑定,而是用
    PHP concat
    select a.* 
    from activity a
    join tags t
    on t.activityID=a.activityId
    where t.tagID in ('sightseeing','parks')
    and t.placeID in ('Istanbul','Paris');
    
    +----+------------+---------+---------------------------+
    | ID | activityID | placeID | summary                   |
    +----+------------+---------+---------------------------+
    |  4 | 444        | Paris   | See Arc D'Triumph         |
    |  6 | 666        | Paris   | See Eifel Tower           |
    |  8 | 888        | Paris   | Walk through Central Park |
    +----+------------+---------+---------------------------+
    3 rows in set (0.01 sec)