Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MySQL: How to get result from prepared statement? [duplicate]

Tags:

php

mysql

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

like image 830
soonoo Avatar asked Oct 24 '25 19:10

soonoo


1 Answers

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().

like image 57
Megh Vidani Avatar answered Oct 26 '25 08:10

Megh Vidani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!