mysqli查询里面有mysql函数

时间:2016-07-26 13:09:55

标签: php mysql mysqli

我正在尝试使用我发现的函数在数据库上生成一个passfrase。 如果我直接在数据库上使用sql查询,它会根据需要生成一个字符串,但是在我的php页面中它给出了错误

  

致命错误:未捕获错误:调用成员函数fetch_array()   在布尔“

$query_rsCaptchaID = "DELIMITER $$
DROP FUNCTION IF EXISTS `randomPasswordGenerator` $$
CREATE FUNCTION `randomPasswordGenerator`(
  ) RETURNS varchar(64) CHARSET utf8
BEGIN
  DECLARE charCount TINYINT(1) DEFAULT 0;
  DECLARE charDiceRoll TINYINT(2);
  DECLARE randomChar CHAR(1);
  DECLARE randomPassword CHAR(64) DEFAULT '';
  REPEAT
    SET charCount = charCount + 1;
    SET charDiceRoll = 1 + FLOOR(RAND() * 94);
    IF (charDiceRoll <= 32)
    THEN
      SET randomChar = ELT(charDiceRoll,
      '`', '~', '!', '@', '#', '$', '%', '^',
      '&', '*', '(', ')', '-', '=', '_', '+',
      '[', ']', '{', '}', '\\', '/', '|', '?',
      ';', ':', '\'', '\"', ',', '.', '<', '>');
    ELSEIF (charDiceRoll >= 33)
      AND (charDiceRoll <= 68)
    THEN
      SET charDiceRoll = charDiceRoll - 33;
      SET randomChar = CONV(
        charDiceRoll,
        10, 36);
    ELSE
      SET charDiceRoll = charDiceRoll - 59;
      SET randomChar = LOWER(
        CONV(
          charDiceRoll,
          10, 36)
      );
    END IF;
    SET randomPassword = CONCAT(randomPassword, randomChar);
  UNTIL (charCount = 64)
  END REPEAT;
  RETURN randomPassword;
END $$
DELIMITER ;
SELECT randomPasswordGenerator() AS captchaID;";
$rsCaptchaID = mysqli_query($db,$query_rsCaptchaID);
$row_rsCaptchaID = $rsCaptchaID->fetch_array();

任何想法?我是MySQLi的新手,而不是说英语,抱歉有错误。

1 个答案:

答案 0 :(得分:0)

发生该错误是因为您的查询失败。在您发布的最后两行之间,您需要进行一些错误检查:

$rsCaptchaID = mysqli_query($db,$query_rsCaptchaID);
if(!$rsCaptchaID) die(mysqli_error($db); // <- will show you the SQL error
$row_rsCaptchaID = $rsCaptchaID->fetch_array();