You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用bind_param()触发布尔值错误,求SQL预处理语句排查方案

Fixing "Call to a member function bind_param() on boolean" Error in Your MySQLi Code

Hey there! That error pops up because your $conn->prepare() call for the UPDATE statement is returning false instead of a valid statement object. When you try to run bind_param() on that boolean value, PHP throws the fatal error you’re seeing. Let’s break down why this happens and fix it step by step.

First, Diagnose the Prepare Failure

The fastest way to find out why your prepare is failing is to add an error check right after calling prepare(). This will show you the exact MySQL error causing the problem. Modify your UPDATE section like this:

$stmtUpdate = $conn->prepare("UPDATE db_control SET cve_usuario=? WHERE cve_control=1");
if (!$stmtUpdate) {
    die("Prepare failed for UPDATE: " . $conn->error);
}
$stmtUpdate->bind_param("i", $r);
if($stmtUpdate->execute()) {
    echo "Update successful!";
} else {
    echo "Execute failed: " . $stmtUpdate->error;
}

I used a separate variable name ($stmtUpdate) instead of reusing $stmt—this avoids confusion with your SELECT statement and prevents accidental issues with active result sets, which is a common pitfall.

Key Issues to Check

  1. Database Permissions: Double-check that your database user (usuario) has UPDATE permissions on the db_control table. Even if you can SELECT data, you might not have write access.
  2. Undefined $r Value: If your initial SELECT query returns no rows, $r will be undefined. Trying to increment it ($r = $r +1) will throw a warning, and passing an undefined value to bind_param() can cause unexpected behavior. Add a fallback:
    $r = 0; // Default value if no row is found
    if($stmtSelect->execute()) {
        $stmtSelect->bind_result($r);
        $stmtSelect->fetch();
    }
    $r = $r + 1;
    
  3. Missing WHERE in SELECT: Your original SELECT query pulls all rows from db_control, then fetches the first one. But you’re updating the row where cve_control=1—so you should add that WHERE clause to your SELECT to make sure you’re incrementing the correct value.

Full Updated Code

Here’s your code with all fixes applied, plus better resource management:

<?php 
$servername = "localhost"; 
$username = "usuario"; 
$password = "usuario"; 
$database = "proyectofinal"; 

// Create connection
$conn = new mysqli($servername, $username, $password, $database); 

// Check connection
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

// Select the specific cve_usuario we need to update
$stmtSelect = $conn->prepare("SELECT cve_usuario FROM db_control WHERE cve_control=1");
if (!$stmtSelect) {
    die("Prepare failed for SELECT: " . $conn->error);
}

$r = 0; // Default value if no row exists
if($stmtSelect->execute()) { 
    $stmtSelect->bind_result($r); 
    $stmtSelect->fetch(); 
} 
$stmtSelect->close(); // Free resources from the select statement

$r = $r + 1; 
echo "<br>" . $r; 

// Update the value in the database
$stmtUpdate = $conn->prepare("UPDATE db_control SET cve_usuario=? WHERE cve_control=1"); 
if (!$stmtUpdate) {
    die("Prepare failed for UPDATE: " . $conn->error);
}
$stmtUpdate->bind_param("i", $r); 
if($stmtUpdate->execute()) { 
    echo "<br>Update completed successfully!";
} else {
    echo "<br>Update failed: " . $stmtUpdate->error;
}

// Clean up
$stmtUpdate->close();
$conn->close();
?>

内容的提问来源于stack exchange,提问作者Alexis Escalona

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:42:00