views:

271

answers:

5

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

With the 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 that I can implode and use in a 3rd query to pull names from table3.

+1  A: 

Isn't it possible to do this in the mysql query?

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')
Erik
A: 

Or you could use your way, in that case you just need to use the same IN statement for mysql.

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

Probably you're getting a type mismatch error in SQL. Your second sql query should be:

$sql = "SELECT SIteID FROM bookings WHERE BSH_ID = '1' AND MarketID IN ($comma_separated)";
xaguilars
+2  A: 

I'd be surprised if this is what you mean:

... AND MarketID = '$comma_separated'

This is saying that the MarketID is stored in the database as a comma separated list. Probably you mean this:

... AND MarketID IN ($comma_separated)

Also, you don't need to make multiple queries, you can use a 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'

Similarly you can join your table3 with this instead to get your final result in one query instead of three.

Mark Byers
+1, join is the correct suggestion
stereofrog
Thanks, I havn't ever had to work with so many tables theres over 20 so it got a bit overwhelming for me.
jason
Had to do a temp table to finally make it happen smoothly since the bookings table has so much data.Again thanks, I never used JOIN so learning that just made things alot easier.
jason
@jason: If your query is running too slowly, it's most likely because you are missing an important index or because you are querying in such a way that the index cannot be used. You probably don't need to make a temp table, you just need to add the correct indexes or fix your query. I think it's a little too in-depth and off-topic for this question to explain how in much more detail, but I'd advise you to run EXPLAIN on your query and learn how to interpret the results of that. You could ask a new question to ask how to speed up one of your slow queries if you are having trouble.
Mark Byers
A: 

The others already wrote about the SQL query. But make also sure that you quote array indexes correctly. So instead of

$SIteID[] = $newArray[SIteID];

do this

$SIteID[] = $newArray['SIteID'];
Felix Kling