0

I am having an issue with a while statement only returning one result. this is my first time trying to display product information from several tables and I don't know what I'm doing wrong. here is the code:

<?php
    require('./includes/config.inc.php');
    require(MYSQL);

    $sql = sprintf("SELECT * FROM tea");

    $res = mysqli_query($dbc, $sql);

    if(!$res){
    die('Could not complete query: '.mysqli_error($dbc));
    } else {

    echo 'Success!<br />';

    while($row = mysqli_fetch_array($res)){
        $table = $row['category'];
        $inum = $row['item_number'];

        echo $table.' '.$inum.'<br />';

        $fetch = sprintf("SELECT sub_category, item_name, description FROM $table WHERE item_number ='$inum'");
        $fetchRes = mysqli_query($dbc, $fetch);

        if(!$fetchRes){
            die('Could not fetch: '.mysqli_error($dbc));
        } else {
            while($fetchRow = mysqli_fetch_array($fetchRes)){
                $subCat = $fetchRes['sub_category'];
                $iNumber = $inum;
                $iname = $fetchRes['item_name'];
                $desc = $fetchRes['description'];

                echo $id.' '.$subCat.' '.$iNumber.' '.$iname.' '.$desc.'<br />';
            }
        }
    }
}

2 Answers 2

2

Inside your while loop, you should be using the $fetchRow variable as the array from which to grab columns, such as 'sub_category'.

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

1 Comment

WOW don't know how I didn't notice that before... THANKS!
0

IS

  $subCat = $fetchRes['sub_category'];
  $iNumber = $inum;
  $iname = $fetchRes['item_name'];
  $desc = $fetchRes['description'];

Needs to be: Res to Row

 $subCat = $fetchRow['sub_category'];
 $iNumber = $inum;
 $iname = $fetchRow['item_name'];
 $desc = $fetchRow['description'];

Comments

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.