Jump to content

PHP Problem


Snodge

Recommended Posts

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 by Snodge
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.