0

我对仅返回一个结果的 while 语句有疑问。这是我第一次尝试从多个表中显示产品信息,但我不知道自己做错了什么。这是代码:

<?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 />';
            }
        }
    }
}
4

2 回答 2

2

在您的 while 循环中,您应该使用$fetchRow变量作为从中获取列的数组,例如“sub_category”。

于 2012-10-02T21:49:59.750 回答
0

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

需要:Res to Row

 $subCat = $fetchRow['sub_category'];
 $iNumber = $inum;
 $iname = $fetchRow['item_name'];
 $desc = $fetchRow['description'];
于 2012-10-02T21:52:34.333 回答