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

2021-12-25 00:00:00 php mysqli

我知道此代码在我拥有的另一个网站上有效,但今天无法正常运行.我收到三个警告:

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

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

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

警告: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 第 35 行

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:

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

Warning: 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?

推荐答案

添加更多错误处理.
当连接失败时,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 ;-)

相关文章