1

I'm trying to fetch row so i can print it on my table. But the problem is that, the code only runs on the 1st while loop. After it fetch all the rows in the table, it stops there.

`Assuming that I have 5 rows in my tblUnit

while ($r = $q->fetch(PDO::FETCH_ASSOC)) {
    echo " <script type'text/javascript'> alert('1');</script>";
    $unitStatus = $r['unitStatus'];
    $unitNumber = $r['unitNumber'];
    $floorNumber = $r['floorCode'];
    $unitType = $r['unitType'];
    $t = $db->query("select floorLevel from tblFloors where floorID = $floorNumber");
    $w = $db->query("select unitTypeName from tblUnitType where unitTypeID = $unitType");
    while ($u = $t->fetch(PDO::FETCH_ASSOC)) {
        echo " <script type'text/javascript'> alert('2');</script>";
        $floorLevel = $u['floorLevel'];
        while ($x = $w->fetch(PDO::FETCH_ASSOC)) {
            echo " <script type'text/javascript'> alert('3');</script>";
            ....
            ...
            ..
        }
    }
}

The output(In alert box):

1
1
1
1
1

I'm using MS SQL Server for my database.

1
  • 2
    So many nested whiles .. are you sure, one query using JOIN wouldn't simplify this? Commented Mar 11, 2016 at 11:12

1 Answer 1

2

Could be a string evalueting problem try thsi way

$t = $db->query("select floorLevel from tblFloors where floorID = " .  $floorNumber .";");                              
$w = $db->query("select unitTypeName from tblUnitType where unitTypeID = " . $unitType . ";");  

or

$t = $db->query("select floorLevel from tblFloors where floorID = '$floorNumber' ;");       

Otherwise your $floorNumber don't contain the aspected result

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! In my database, there were no matching value for floorID recorded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.