致命错误:在布尔值上调用成员函数execute()

致命错误:在布尔值上调用成员函数execute()

问题描述:

<?php
    session_start();
    $config = parse_ini_file('../database_config.ini'); 
    //Create Database connection
    $connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);

    if ($connection->connect_error) {
        die('Could not connect to db: ' . mysql_error());
    }


    $stmt = $connection->prepare("INSERT INTO report (reportID, userID, description, address, postalcode, latitude, longitude) 
    VALUES(0, 007, 'Major fire', 'Jurong Point', 640724, 1.640724, 103.640724)");

        $stmt->execute();
        echo "Error:
";
        print_r($stmt->error_list);
        $stmt->close();

        $connection->close();
?>

Error : Fatal error: Call to a member function execute() on boolean

Why does my prepare statement fail?

Structure of my report table enter image description here

 &lt;?php 
 session_start(); 
 $ config = parse_ini_file('../  database_config.ini');  
 //创建数据库连接
 $ connection = mysqli_connect('localhost',$ config ['username'],$ config ['password'],$ config ['dbname']); 
 
 if($  connection-&gt; connect_error){
 die('无法连接到db:'。mysql_error()); 
} 
 
 
 $ stmt = $ connection-&gt; prepare(“INSERT INTO report(reportID)  ,用户ID,描述,地址,邮政编码,纬度,经度)
 VALUES(0,007,'Major fire','Jurong Point',640724,1.640724,103.640724)“); 
 
 $ stmt-&gt;执行 (); 
 echo“错误:
”; 
 print_r($ stmt-&gt; error_list); 
 $ stmt-&gt; close(); 
 
 $ connection-&gt; close(); \  n?&gt; 
  code>  pre> 
 
 

错误:致命错误:在布尔 code>上调用成员函数execute() p> \ n

为什么我的预备语句失败? p>

我的 report code>表的结构 div>

You need to do it in following manner:-

<?php
    session_start();
    $config = parse_ini_file('../database_config.ini'); 
    //Create Database connection
    $connection = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);

    if (mysqli_connect_errno()) { // check the change of if condition
        printf("Connect failed: %s
", mysqli_connect_error());
        exit();
    }
    $stmt = mysqli_prepare($connection,"INSERT INTO report (reportID, userID, description, address, postalcode, latitude, longitude) VALUES(0, 007, 'Major fire', 'Jurong Point', 640724, 1.640724, 103.640724)"); // check the change in query code

    mysqli_stmt_execute($stmt); // check the change in execution code
    echo "Error:
";
    print_r(mysqli_error($connection)); // check the change in error getting code
    mysqli_stmt_close($stmt);// check the change in statement closing code
    mysqli_close($connection); // check the change in db connection closing code
?>

For more knowledge refer link and it's example:- http://php.net/manual/en/mysqli.prepare.php

You are combining Object Oriented style with the normal procedural mysqli style. On line 5 you use.

mysqli_connect()

and on line 12 you use.

$connection->prepare()

This will not work, if you'd change $connection to, object oriented style like you do with your prepare statement, it will work.

$connection = new mysqli('localhost', $config['username'], $config['password'], $config['dbname'])

More information can be found here http://php.net/manual/en/mysqli.prepare.php