Put query results in array and then fix?

Basically I pull the identifier from table1, use this identifier to find the site identifier in table2, then I need to use the site identifiers in the array, implode and query table3 for the site names. I cannot blow the array correctly, first I got an error and then used a while loop.

In a while loop, the output simply says: 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);

?>

Basically, what I need to do is correctly move the query results to an array, which I can decouple and use in the third query to pull the names from the table3.

+3
source share
5 answers

, , :

... 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'

3 , .

+2

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')
+1

, IN mysql.

$sql = "SELECT SIteID FROM bookings WHERE BSH_ID = '1' AND MarketID IN (".$comma_separated.")";
0

, SQL. sql :

$sql = "SELECT SIteID FROM bookings WHERE BSH_ID = '1' AND MarketID IN ($comma_separated)";
0

SQL-. , .

$SIteID[] = $newArray[SIteID];

$SIteID[] = $newArray['SIteID'];
0

Source: https://habr.com/ru/post/1739272/


All Articles