Search code examples
phpmysqliprepared-statement

Unable to run query using prepared statement in MySQLi


I am working on the below code. Why am I not able to run the query properly? I already check the database connection and it is fine

 <?php

 $sql = "SELECT dt, events, eventtype FROM events";
 $stmt = $mysqli->prepare($sql);
        $stmt->execute();
        $stmt->bind_result($dt,$events,$eventtype);
        $stmt->store_result();
        if($stmt->num_rows >0)  {
            $stmt->fetch();
        }
            else {
                echo "Cant Find The data!";
            }
$stmt->close();
$mysqli->close();
        echo $dt;
        echo $events;
        echo $eventtype;
?>

getting this error

Fatal error : Call to a member function execute() on boolean in /srv/disk1/2555378/www/domain.net/index.php on line 113


Solution

  • This means that the variable $mysqli contains a boolean value, probably false.

    According to the php docs, http://php.net/manual/en/mysqli.prepare.php, the function mysqli::prepare will return false in case of an error.

    You should use the error variable to get more information, like here: http://php.net/manual/en/mysqli.error.php