本文介绍了Mysqli引发“警告:mysqli_stmt_bind_param()期望参数1为mysqli_stmt,给定布尔值".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这段代码可以在我拥有的另一个站点上运行,但是今天还不能发挥作用.我收到三个警告:

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

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

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

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

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

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

Can someone help me figure this out?

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

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

$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";

编辑

经过一番调查后发现,prepare语句在mysql4中不起作用.我创建了一个新的mysql5数据库,但现在尝试连接时出现此错误:

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:

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

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

推荐答案

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

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).

要进行测试,您可以使用以下内容:

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 ;-)

这篇关于Mysqli引发“警告:mysqli_stmt_bind_param()期望参数1为mysqli_stmt,给定布尔值".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 18:59