简体   繁体   English

PHP MySQL:如何从准备好的语句中获取结果?

[英]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. echo $stmt->num_rows打印正确的值,但是我无法从 while 语句中获得结果。 I also tried mysqli::bind_result but didn't work.我也试过mysqli::bind_result但没有用。
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() .要获取,您需要使用$stmt->fetch()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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