2012-10-02 185 views
0

我有一個while語句只返回一個結果的問題。這是我第一次嘗試顯示來自多個表格的產品信息,我不知道我在做什麼錯誤。這裏是代碼:php while循環只顯示多個表中的mysql查詢的一個結果

<?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

內部while循環,你應該使用$fetchRow變量作爲數組從中攫取列,如「sub_category」。

+0

WOW不知道怎麼我沒有之前看到...謝謝! –

0

IS

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

需要是:RES到行

$subCat = $fetchRow['sub_category']; 
$iNumber = $inum; 
$iname = $fetchRow['item_name']; 
$desc = $fetchRow['description']; 
相關問題