php - how to retrieve data using two while loops -
the first query works fine , retrieves class id have problem second query not retrieve child name
<select name="child-name" id="schild"> <?php $username = $_session['username']; $sql1 = "select class_id class emp_id = '$username'"; $result = mysqli_query($database,$sql) or die(mysqli_error($database)); while($row = mysqli_fetch_array($result)) { $classid = $row['class_id']; $sql2 = "select name children class_id = '$classid'"; $result2 = mysqli_query($database,$sql2) or die(mysqli_error($database)); while($row = mysqli_fetch_array($result2)) ?> <option><?php echo $row['name'];?></option> <?php } ?> </select>
you using $row in both resultset fetches. therefore destroying outer loop
while testing add these lines, specially if testing on live/hosted server error reporting turned off
<?php ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(e_all); mysqli_report(mysqli_report_error | mysqli_report_strict); ?> <select name="child-name" id="schild"> <?php $username = $_session['username']; $sql1 = "select class_id class emp_id = '$username'"; $result = mysqli_query($database,$sql1) or die(mysqli_error($database)); // late breaking fix ^^^^ while($row = mysqli_fetch_array($result)) { $classid = $row['class_id']; $sql2 = "select name children class_id = '$classid'"; $result2 = mysqli_query($database,$sql2) or die(mysqli_error($database)); while($row2 = mysqli_fetch_array($result2)) { // fix ^^^^^ ?> <option value="<?php echo $row2['name'];?>"><?php echo $row2['name'];?></option> <?php // fix ^^^^^ } ?> </select> <?php // add terminator first while loop } ?>
Comments
Post a Comment