Retrieve data from database using mysqli

2019-09-26 09:58发布

问题:

I have a problem when I fetching data from database and row it I Got an error. How To fetch data From database using while and row it.

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

Thanks in advance..

Here is my Code..

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

回答1:

You seem to be mixing up mysqli_stmt and mysqli_result methods.

Try making the following changes:

  1. LIMIT your resultset to one

    ... FROM table_inventory WHERE id = ? LIMIT 1
    
  2. Stick with the mysqli_stmt as you seem to be further along with that

    $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 ...
    


标签: php mysqli