基本上我从table1中提取Id,使用该id在table2中查找站点ID,然后需要在数组中使用站点ID,implode和查询table3以获取站点名称。我不能先正确地破坏数组,先得到错误,然后使用while循环。
使用while循环,输出只是说:Array
$mysqli = mysqli_connect("server", "login", "pass", "db");
$sql = "SELECT MarketID FROM marketdates WHERE Date = '2010-04-04 00:00:00' AND VenueID = '2'";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
$dates_id = mysqli_fetch_assoc ( $result );
$comma_separated = implode(",", $dates_id);
echo $comma_separated; //This Returns 79, which is correct.
$sql = "SELECT SIteID FROM bookings WHERE BSH_ID = '1' AND MarketID = '$comma_separated'";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
// This is where my problems start
$SIteID = array();
while ($newArray = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$SIteID[] = $newArray[SIteID];
}
$locationList = implode(",",$SIteID);
?>
基本上我需要做的就是将查询结果正确地移动到一个我可以内爆的数组,并在第三个查询中用来从table3中提取名称。
答案 0 :(得分:2)
... AND MarketID = '$comma_separated'
这就是说MarketID作为逗号分隔列表存储在数据库中。可能你的意思是:
... AND MarketID IN ($comma_separated)
此外,您不需要进行多次查询,可以使用JOIN:
SELECT SIteID
FROM marketdates
JOIN bookings
ON marketdates.MarketID = bookings.MarketID
WHERE bookings.BSH_ID = '1'
AND marketdates.Date = '2010-04-04 00:00:00'
AND marketdates.VenueID = '2'
同样,您可以将table3与此相关联,以便将最终结果放在一个查询中,而不是三个。
答案 1 :(得分:1)
是不是可以在mysql查询中执行此操作?
SELECT SIteID FROM bookings WHERE BSH_ID = '1' AND MarketID IN (SELECT MarketID FROM marketdates WHERE Date = '2010-04-04 00:00:00' AND VenueID = '2')
答案 2 :(得分:0)
或者您可以使用自己的方式,在这种情况下,您只需要为mysql使用相同的IN语句。
$sql = "SELECT SIteID FROM bookings WHERE BSH_ID = '1' AND MarketID IN (".$comma_separated.")";
答案 3 :(得分:0)
您可能在SQL中遇到类型不匹配错误。你的第二个SQL查询应该是:
$sql = "SELECT SIteID FROM bookings WHERE BSH_ID = '1' AND MarketID IN ($comma_separated)";
答案 4 :(得分:0)
其他人已经写过关于SQL查询的文章。但请确保正确引用数组索引。而不是
$SIteID[] = $newArray[SIteID];
这样做
$SIteID[] = $newArray['SIteID'];