2014-02-21 32 views
-3

當我從數據庫中獲取數據並對其進行排序時遇到問題我出現錯誤。 如何使用while從數據庫中獲取數據並對其進行排序。使用mysqli從數據庫檢索數據

Fatal error: Call to a member function fetch() on a non-object in

在此先感謝..

這是我的代碼..

<body> 
<?php 

$id = $_GET['id']; 

$iqry = $mysqli->prepare("SELECT itemcode,itemname,brandname,quantity FROM table_inventory WHERE id = ?"); 
$iqry->bind_param('i', $id); 
$iqry->execute(); 
$iqry->bind_result($itemcode,$itemname,$brandname,$quantity); 
$res = $iqry->store_result(); 

while ($row = $res->fetch()){ 
    $itemcode = $row['itemcode']; 
    $itemname = $row['itemname']; 
    $brandname = $row['brandname']; 
    $quantity = $row['quantity']; 
} 
?> 

    <form method="post" name="increasing" action="save.php"> 
     <table> 
      <tr><td>Itemcode</td><td>:</td><td><input type="text" name="itemcode" required="required" value="<?php echo $itemcode; ?>">/></td></tr> 
      <tr><td>Itemname</td><td>:</td><td><input type="text" name="itemname" required="required" value="<?php echo $itemname; ?>"/></td></tr> 
      <tr><td>Brandname</td><td>:</td><td><input type="text" name="brandname" required="required" value="<?php echo $brandname; ?>"/></td></tr> 
      <tr><td>Quantity</td><td>:</td><td><input type="text" name="quantity" required="required" value="<?php echo $quantity; ?>"/></td></tr> 
      <tr><td></td><td></td><td><input type="submit" class="myButton" value="Save"/></td></tr> 
     </table> 
    </form> 
<body> 
+0

有你諮詢了mysqli手冊? – Trent

+0

將您的錯誤消息插入Google,然後選擇可激發您興趣的任何結果 - 數以百計! – 2014-02-21 02:20:57

+0

你應該閱讀['mysqli_stmt :: store_result'](http://php.net/manual/mysqli-stmt.store-result.php)的手冊 – Phil

回答

0

你似乎混淆mysqli_stmtmysqli_result方法。

嘗試做如下改變:

  1. LIMIT您的結果集一個

    ... FROM table_inventory WHERE id = ? LIMIT 1 
    
  2. 堅持與mysqli_stmt你似乎與

    $iqry->bind_param('i', $id); 
    $iqry->execute(); 
    $iqry->bind_result($itemcode, $itemname, $brandname, $quantity); 
    
    if (!$iqry->fetch()) { 
        throw new Exception('No results found for ID ' . $id, 404); 
    } 
    
    // You can now use $itemcode, $itemname, $brandname and $quantity, 
    // they will all be set 
    
    ?> 
    
    <form ... 
    
一起進一步爲
+0

謝謝幫助 但我得到一個錯誤。 致命錯誤:在堆棧跟蹤中發現未找到異常'異常'消息'ID爲'未找到結果':#0 {main}拋出 並嘗試將您的建議添加到我的代碼中 '$ iqry = $ mysqli-> prepare 「SELECT itemcode,itemname,brandname,quantity FROM table_inventory WHERE id =?LIMIT 1」); $ iqry-> execute(); $ iqry-> bind_result($ itemcode,$ itemname,$ brandname,$ quantity); ('$ iqry-> fetch()){ }拋出新的異常('沒有找到ID的結果'。$ id,404); } while($ row = $ iqry-> fetch()){ $ itemcode = $ row ['itemcode'];}' 對不起對於新手問題 – user3335368

+0

@ user3335368真的很明顯。你的'$ id'參數是空的。您的網址中可能沒有'?id ='。 *其實*,它看起來像你已經錯過了'bind_param'行 – Phil

+0

謝謝答覆先生@Phil .. 但我不知道如何做到這一點.. 我試圖再次檢索,但沒有錯誤,沒有結果發現.. – user3335368