简体   繁体   中英

PHP MySQL: How to get result from prepared statement?

 $conn = new mysqli(.....);
 $param = $_GET['manf'];

 $stmt = $conn->prepare('select manf from manf where manf = ?');
 $stmt->bind_param('s', $param);
 $stmt->execute();
 $stmt->store_result();

 echo $stmt->num_rows;

 $result = $stmt->get_result();

 if(!$result){
     die(mysql_error());
 }
 while($row = $result->fetch_assoc()){
     echo $row['manf'];
 }

echo $stmt->num_rows prints right vaule however I can't get results from while statement. I also tried mysqli::bind_result but didn't work.
How can I fix this?

Try this:

$conn = new mysqli(.....);
$param = $_GET['manf'];

$stmt = $conn->prepare('select manf from manf where manf = ?');
$stmt->bind_param('s', $param);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($result);

echo $stmt->num_rows;

while($stmt->fetch()){
    echo $result;
}
$stmt->free_result();
$stmt->close();

for fetching you need to use $stmt->fetch() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM