-1

I have below query:

//Get the data
$stmt = $dbh->prepare("
SELECT t1.*, t2.*
FROM softbox_bookings_data t1
LEFT JOIN softbox_bookings t2 ON t1.booking_id = t2.id
WHERE t2.id=:id
GROUP BY t1.model
ORDER BY t2.time ASC");
$stmt->bindParam(":id", $_GET["id"]);
$stmt->execute();
$data = $stmt->fetchAll();

Now I want to be able to use above query multiple times.

First I want to just echo out a variable:

echo $data["name"];  //Returns nothing

Then further down the page, I want to loop the data:

foreach($data as $row){
    echo $row["name"]; //Successfully echoes out the name from the database.
}

How come I can't use the echo $data["name"] variable and the foreach() statement to run through the data?

oliverbj
  • 5,771
  • 27
  • 83
  • 178

1 Answers1

0

Turn on error reporting and always use var_dump instead of echo when checking variable values. The latter will only print strings, but data is a two-dimensional array. Just check the var_dump output.

BTW: Of course echo "returns" nothing, its return type is void.

Community
  • 1
  • 1
steffen
  • 16,138
  • 4
  • 42
  • 81