Home  >  Article  >  Backend Development  >  Why Am I Getting a \"Number of Bind Variables Doesn\'t Match Number of Fields\" Error in My INSERT Query?

Why Am I Getting a \"Number of Bind Variables Doesn\'t Match Number of Fields\" Error in My INSERT Query?

Susan Sarandon
Susan SarandonOriginal
2024-10-28 09:41:29966browse

Why Am I Getting a

INSERT Query Raises Issue: Number of Bind Variables Incompatible with Prepared Statement

In your PHP code, an error message indicates a mismatch between the number of bind variables and fields in a prepared statement. Specifically, the warning reads:

"Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement"

This error occurs in the following code:

if($stmt = $conn -> prepare("INSERT INTO login(user, pass) VALUES(?, ?)")) {

  /* Bind parameters s - string, b - blob, i - int, etc */
  $stmt -> bind_param("ss", $user, $pw);

  /* Execute it */
  $stmt -> execute();

  /* Bind results */
  $stmt -> bind_result($user, $pw);

  /* Close statement */
  $stmt -> close();
  $userId = $conn->insert_id;
}

Understanding the Issue

The error stems from your attempt to bind result variables ($user and $pw) to a statement that does not return any results. In this case, the INSERT query does not have a SELECT clause or a stored procedure that returns values. Therefore, binding result variables is unnecessary.

Resolving the Issue

To resolve the issue, simply remove the line that binds result variables:

$stmt -> bind_result($user, $pw);

By removing this line, you eliminate the mismatch between the number of bind variables (0) and the number of fields in the prepared statement (also 0). This should resolve the error and allow your code to execute successfully.

The above is the detailed content of Why Am I Getting a \"Number of Bind Variables Doesn\'t Match Number of Fields\" Error in My INSERT Query?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn