PHP:语句无法准备,但SQL是正确的

时间:2017-03-23 22:22:26

标签: php mysql mysqli

所以MySQLi语句无法准备错误,说它的语法不正确。但是,当我复制准备好的语句并直接在dbhost上使用它时,它可以正常工作。

这是准备的代码。

function sanitiseStatement($sql, $params)
{
  global $mysqli;

  $stmt = mysqli_stmt_init($mysqli);
  if (mysqli_stmt_prepare($stmt, $sql))
  {
    call_user_func_array(array($stmt, 'bind_param'), refValues($params));

    return $stmt;
  }
  error_log("Failed to prepare statement: ".mysqli_stmt_error($stmt));
  die ("Failed to prepare statement: ".mysqli_stmt_error($stmt));
}

这是传递给它的SQL语句。

START TRANSACTION; INSERT INTO Recipe (UserID, RecipeName, Difficulty, Servings) VALUES (?, ?, ?, ?); SET @recipe_ID = LAST_INSERT_ID(); INSERT INTO Instructions (InstructionNumber, RecipeID, Instruction, Duration) VALUES (?, @recipe_ID, ?, ?);  INSERT IGNORE INTO Ingredients (IngredientName) VALUES (?);  INSERT INTO RecipeIngredients (RecipeID , IngredientID,Quantity, Required) SELECT @recipe_ID AS RecipeID, IngredientID, ? AS quantity, ? AS required FROM Ingredients WHERE IngredientName = ?;  COMMIT;

这是错误日志和参数。

[Thu Mar 23 22:10:08 2017] 0: i => 2
[Thu Mar 23 22:10:08 2017] 1: s => Test
[Thu Mar 23 22:10:08 2017] 2: i => 1
[Thu Mar 23 22:10:08 2017] 3: i => 3
[Thu Mar 23 22:10:08 2017] 4: i => 1
[Thu Mar 23 22:10:08 2017] 5: s => Method
[Thu Mar 23 22:10:08 2017] 6: i => 2
[Thu Mar 23 22:10:08 2017] 7: s => Bacon
[Thu Mar 23 22:10:08 2017] 8: s => 1
[Thu Mar 23 22:10:08 2017] 9: i => 0
[Thu Mar 23 22:10:08 2017] 10: s => Bacon
[Thu Mar 23 22:10:08 2017] Failed to prepare statement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO Recipe (UserID, RecipeName, Difficulty, Servings) VALUES (?, ?, ?, ?' at line 1

我很困惑,因为我在SQL语句中找不到错误,并且在将它直接与数据库一起使用时似乎有效。

0 个答案:

没有答案