调用bind_param()触发布尔值错误,求SQL预处理语句排查方案
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
- Database Permissions: Double-check that your database user (
usuario) has UPDATE permissions on thedb_controltable. Even if you can SELECT data, you might not have write access. - Undefined
$rValue: If your initial SELECT query returns no rows,$rwill be undefined. Trying to increment it ($r = $r +1) will throw a warning, and passing an undefined value tobind_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; - Missing
WHEREin SELECT: Your original SELECT query pulls all rows fromdb_control, then fetches the first one. But you’re updating the row wherecve_control=1—so you should add thatWHEREclause 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

