PHP-将查询结果放入数组,然后放大?

前端之家收集整理的这篇文章主要介绍了PHP-将查询结果放入数组,然后放大? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

基本上,我从table1中提取一个ID,使用该ID在table2中查找站点ID,然后需要在数组中使用站点ID,内爆并在table3中查询站点名称.我无法正确内爆数组,首先我遇到错误,然后使用while循环.

使用while循环,输出仅表示:数组

$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中提取名称.

最佳答案
如果这是您的意思,我会感到惊讶:

... 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与此连接,以在一个查询中而不是三个查询中获得最终结果.

原文链接:https://www.f2er.com/mysql/532072.html

猜你在找的MySQL相关文章