php Mysqli 抛出“警告:mysqli_stmt_bind_param() 期望参数 1 为 mysqli_stmt,给出布尔值”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2228430/
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-25 05:38:22  来源:igfitidea点击:

Mysqli throws "Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given"

phpmysqli

提问by Drew

I know that this code works on another site I've got but it's not playing ball today. I get three warnings:

我知道这段代码可以在我拥有的另一个网站上运行,但今天不能正常工作。我收到三个警告:

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php on line 33

Warning: mysqli_execute() expects parameter 1 to be mysqli_stmt, boolean given in /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php on line 34

Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given in /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php on line 35

警告:mysqli_stmt_bind_param() 期望参数 1 为 mysqli_stmt,布尔值在 /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php 第 33 行

警告:mysqli_execute() 期望参数 1 为 mysqli_stmt,布尔值在 /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php 第 34 行

警告:mysqli_stmt_affected_rows() 期望参数 1 为 mysqli_stmt,布尔值在 /homepages/14/d248783986/htdocs/subdomains/clients.bionic-comms.co.uk/httpdocs/carefree/process.php 第 35 行

Can someone help me figure this out?

有人可以帮我解决这个问题吗?

I am having to use htaccess to upgrade to PHP5 if this helps.

如果这有帮助,我必须使用 htaccess 升级到 PHP5。

$connection = mysqli_connect($hostname, $username, $password, $dbname);

if (!$connection) {
    die('Connect Error: ' . mysqli_connect_error());
}

$query = "INSERT INTO entries (name, dob, school, postcode, date) VALUES (?,?,?,?,?)";
$stmt1 = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($stmt1, 'sssss',$name,$dob,$school,$postcode,$date);
mysqli_execute($stmt1);
if(mysqli_stmt_affected_rows($stmt1) != 1)
    die("issues");
mysqli_stmt_close($stmt1);
return "new";

EDIT

编辑

After some investigation it transpires that the prepare statement doesn't play ball with mysql4. I have created a new mysql5 database but I now get this error when I try to connect:

经过一番调查,发现prepare 语句与mysql4 无关。我创建了一个新的 mysql5 数据库,但是当我尝试连接时出现此错误:

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host 'localhost:/tmp/mysql5.sock' (1)

警告: mysqli_connect() [function.mysqli-connect]: (HY000/2005): Unknown MySQL server host 'localhost:/tmp/mysql5.sock' (1)

Does anyone have any idea as to why this is happening?

有没有人知道为什么会这样?

回答by VolkerK

Add more error handling.
When the connection fails, mysqli_connect_error()can tell you more details.
When preparing the statement fails mysqli_error()has more infos about the error.
When executing the statement fails, ask mysqli_stmt_error(). And so on and on...
Any time a function/method of the mysqli module returns false to indicate an error, a) handle that error and b) decide whether it makes sense or not to continue. E.g. it doesn't make sense to continue with database operations when the connection failed. But it may make sense to continue inserting data when just one insertion failed (may make sense, may not).

添加更多错误处理。
当连接失败时,mysqli_connect_error()可以告诉你更多细节。
当准备语句失败时mysqli_error()有关于错误的更多信息。
当执行语句失败时,询问mysqli_stmt_error()。依此类推...
任何时候 mysqli 模块的函数/方法返回 false 以指示错误,a) 处理该错误,以及 b) 决定继续是否有意义。例如,当连接失败时继续进行数据库操作是没有意义的。但是当只有一次插入失败时继续插入数据可能是有意义的(可能有意义,可能没有)。

For testing you can use something like this:

对于测试,您可以使用以下内容:

$connection = mysqli_connect(...);
if ( !$connection ) {
  die( 'connect error: '.mysqli_connect_error() );
}

$query = "INSERT INTO entries (name, dob, school, postcode, date) VALUES (?,?,?,?,?)";
$stmt1 = mysqli_prepare($connection, $query);
if ( !$stmt1 ) {
  die('mysqli error: '.mysqli_error($connection);
}
mysqli_stmt_bind_param($stmt1, 'sssss',$name,$dob,$school,$postcode,$date);
if ( !mysqli_execute($stmt1) ) {
  die( 'stmt error: '.mysqli_stmt_error($stmt1) );
}
...

For a real production environment this approach is to talkative and dies to easily ;-)

对于真实的生产环境,这种方法很健谈,而且很容易死掉;-)

回答by Tatu Ulmanen

The problem comes from mysqli_prepare(), which in your case returns false, hence the 'boolean given' error. As your query seems ok, the error must be in $connection. Are you sure that the connection works and is defined?

问题来自mysqli_prepare(),在您的情况下返回false,因此出现 'boolean given' 错误。由于您的查询似乎没问题,因此错误必须在$connection. 您确定连接有效并已定义?

The connection should be defined as something like this:

连接应该被定义为这样的:

$connection = mysqli_connect("localhost", "my_user", "my_password", "world");

If the connection is properly defined, you can use echo mysqli_connect_error()to see if something went wrong. You said that the code works on another site, so perhaps you forgot to change the credentials or host?

如果正确定义了连接,您可以使用它echo mysqli_connect_error()来查看是否出现问题。您说该代码适用于另一个站点,所以您可能忘记更改凭据或主机?