0

I want to fetch data with the prepared statements but getting the following answer. I also tried to follow previous solved question related to this which was given on StackOverflow but failed. Help me to solve the issue.

Fatal error: Uncaught Error: Call to a member function execute() on bool
<?php
 $stmt = $mysqli->prepare("SELECT id,addpage,post_name FROM detail WHERE id = ? ");
$stmt->execute();

$result = $stmt->get_result();

var_dump($row = $result->fetch_all(MYSQLI_ASSOC));

echo $row['id'];
         echo $row['addpage'];
        echo $row['post_name'];
?>
  • I'm on the fence as to whether or not this is an exact duplicate, but at the very least you need to [get the error from MySQL](https://stackoverflow.com/a/22662582/328193) to find out why the `prepare` call failed. – David Mar 17 '21 at 18:05
  • What have you tried to debug the problem? Have you checked whether there is any row in the database where `id = ?` matches? – Nico Haase Mar 17 '21 at 18:15

1 Answers1

0

mysqli_prepare returns bool (false) on error, in this case likely because you forgot to add the parameters.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $mysqli->prepare("SELECT id,addpage,post_name FROM detail WHERE id = ? ");
$stmt->bind_param('id', $some_defined_id);
$stmt->execute();

$result = $stmt->get_result();

var_dump($row = $result->fetch_all(MYSQLI_ASSOC));

echo $row['id'];
         echo $row['addpage'];
        echo $row['post_name'];

Also, see the documentation at https://www.php.net/manual/en/mysqli.prepare.php

prieber
  • 554
  • 3
  • 16