php 在布尔值上调用成员函数 execute()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35132451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:21:52  来源:igfitidea点击:

Call to a member function execute() on boolean in

phpmysqlmysqli

提问by idkn

My html :

我的 HTML :

 <form action="rent.php" method="post"><pre>
        Email : <input  type="text" name="email">
        Message : <input type="text" name="msg_text">
                <input type="submit" value="Rent it">
    </pre></form>

My rent.php file :

我的rent.php文件:

<?php
 require_once 'login.php';
   $conn = new mysqli($hn, $un, $pw, $db);
   if ($conn->connect_error) {
    die($conn->connect_error);
}
    $query = "SET NAMES utf8";
    $result = $conn->query($query);
    if (!$result) {
        die($conn->error);
    }

    $req = $conn->prepare('INSET INTO renter (email, msg_text) VALUES(?, ?)');
    $req->execute(array($_POST['email'], $_POST['msg_text']));

    header('Location: menu.php');

My error when I try to submit, is : Fatal error: Call to a member function execute() on boolean in C:...\rent.php on line 18

我尝试提交时的错误是:致命错误:调用成员函数 execute() on boolean in C:...\rent.php on line 18

email, msg_text are in varchar type

email, msg_text 是 varchar 类型

回答by tomas.lang

mysqli->preparecan also return FALSE(check http://php.net/manual/en/mysqli.prepare.php) if an error occurred. Your problem is that you have INSETinstead of INSERT.

mysqli->prepare如果发生错误,也可以返回FALSE(检查http://php.net/manual/en/mysqli.prepare.php)。你的问题是你有INSET而不是INSERT.

回答by Dhaval Bhimani

This may help someone: I was facing same issue. In my case, i have missed to close first prepared statement before executing second prepared statement.

这可能对某人有所帮助:我遇到了同样的问题。就我而言,在执行第二个准备好的语句之前,我错过了关闭第一个准备好的语句。

When i have added $select_stmt_type->close();before executing second prepare statement, it fixed the issue.

当我$select_stmt_type->close();在执行第二个准备语句之前添加时,它解决了这个问题。

回答by Vaibs

Just to add. Similar error comes when the mysql column name is incorrect in php. I found below error for my condition when i printed the $conn->error.

只是为了补充。当 php 中的 mysql 列名不正确时,会出现类似的错误。当我打印 $conn->error 时,我发现了以下错误。

errno: 1054, error: Unknown column 'xyz' in 'field list'

错误号:1054,错误:“字段列表”中的未知列“xyz”

回答by Thagana

To catch errors do something like this

要捕获错误,请执行以下操作

    $req = $conn->prepare('INSERT INTO renter (email, msg_text) VALUES(?, ?)');
    if(!$req){
       echo "Prepare failed: (". $conn->errno.") ".$conn->error."<br>";
    }

since prepare returns a boolean. Please use when debugging you code and refer to error reportingas stated in the comment.

因为准备返回一个布尔值。请在调试代码时使用,并参考注释中所述的错误报告

回答by Patrick Janser

Your SQL prepared statement needs to be corrected as shudentsaid, but I think you also have to set the values with ->bind_param()before calling ->execute()(which does not take any parameters).

正如shudent所说,您的 SQL 准备语句需要更正,但我认为您还必须->bind_param()在调用之前设置值->execute()(不带任何参数)。

$stmt = $conn->prepare('INSERT INTO renter (email, msg_text) VALUES(?, ?)');
$stmt->bind_param("ss", $_POST['email'], $_POST['msg_text']);
$stmt->execute();

See documentation and example on the PHP official site : http://php.net/manual/fr/mysqli.prepare.php

请参阅 PHP 官方网站上的文档和示例:http: //php.net/manual/fr/mysqli.prepare.php