Snodge Posted September 28, 2021 Share Posted September 28, 2021 (edited) I can't figure out why this isn't working. Any help is appreciated. $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT id, username FROM usermanagement"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["username"]. " " . $row["lastname"]. "<br>"; } } else { echo "0 results"; } $username = $row["username"]; $sql2 = "SELECT slot_id FROM booking_reservation where reservation_name = $username"; $result2 = $conn->query($sql2); if ($result2->num_rows > 0) { // output data of each row while($row2 = $result2->fetch_assoc()) { echo "Name: " . $row2["slot_id"]. "<br>"; } } else { echo "0 results"; } $sql1 = "SELECT * FROM booking_slots WHERE slot_id = $row2[slot_id]"; $result1 = $conn->query($sql1); if ($result1->num_rows > 0) { // output data of each row while($row1 = $result1->fetch_assoc()) { echo "id: " . $row1["slot_id"]. " - Date: " . $row1["slot_date"]. " " . $row["username"]. "<br>"; } } else { echo "0 results"; } Edited September 28, 2021 by Snodge Quote Link to comment Share on other sites More sharing options...
requinix Posted September 28, 2021 Share Posted September 28, 2021 $row and $row2 both come from while loops. They're only useable within those loops. Your code tries to show reservations for one user, and then booking slots for one reservation. Does that make sense to do? Which user and which reservation are they supposed to show? Quote Link to comment Share on other sites More sharing options...
Barand Posted September 29, 2021 Share Posted September 29, 2021 Also, don't run a series of chained queries where you use the result of one query to determine the records selected in the next. Instead use a single query with JOINs SELECT um.id , um.username , um.lastname , bs.slot_id , bs.slot_date FROM usermanagement um JOIN booking_reservation br ON um.username = br.reservation_name JOIN booking_slots bs ON br.slot_id = bs.slot_id ORDER BY um.username, bs.slot_date Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.