2016-03-31 68 views
0
<?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(); 
?> 

错误的execute():Fatal error: Call to a member function execute() on boolean致命错误:调用一个成员函数上的布尔

为什么我准备发言失败?

reportenter image description here

+1

这与你的'准备'陈述有关,请看这个例子如何准备你的查询:http://www.w3schools.com/php/php_mysql_prepared_statements.asp – Jer

+0

check thi s链接示例: - http://php.net/manual/en/mysqli.prepare.php –

+1

不要在mysql_error())中混合使用mysql和mysqli';' – Saty

回答

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

+0

很高兴帮助你:) :) –

2

的结构您与正常程序的mysqli风格相结合的面向对象的风格。 您使用的第5行。

mysqli_connect() 

和您使用的第12行。

$connection->prepare() 

这是行不通的,如果像你与你准备的语句你会改变$连接,面向对象的风格,它会工作。

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

更多信息可以在这里找到http://php.net/manual/en/mysqli.prepare.php

相关问题