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

时间:2016-03-31 09:34:49

标签: php mysql

<?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:\n";
        print_r($stmt->error_list);
        $stmt->close();

        $connection->close();
?>

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

为什么我的准备声明失败?

report表的结构 enter image description here

2 个答案:

答案 0 :(得分:2)

您正在将面向对象的样式与正常的过程mysqli样式相结合。 在第5行,你使用。

SELECT
     COUNT( id ) AS pages,
     hash,
     MIN( DATE( TIMESTAMP ) ) AS FirstVisit,
     MAX( DATE( TIMESTAMP ) ) AS LastVisit
FROM behaviour 
GROUP BY hash
HAVING MIN( DATE( TIMESTAMP ) ) != MAX( DATE( TIMESTAMP ) )

并在第12行使用。

mysqli_connect()

如果您将$ connection更改为面向对象的样式,就像使用prepare语句一样,这将无效。

$connection->prepare()

可在此处找到更多信息http://php.net/manual/en/mysqli.prepare.php

答案 1 :(得分:1)

您需要按以下方式执行此操作: -

<?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\n", 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:\n";
    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
?>

有关更多知识,请参阅链接及其示例: - http://php.net/manual/en/mysqli.prepare.php