【问题标题】:mysqli query with mysql function inside [duplicate]mysqli查询里面有mysql函数[重复]
【发布时间】:2016-11-30 03:47:26
【问题描述】:

我正在尝试使用我在某些地方找到的函数在数据库上生成密码。 如果我直接在数据库上使用 sql 查询,它会根据需要生成一个字符串,但在我的 php 页面中它会给出 error:

"致命错误:未捕获错误:调用成员函数 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 的新手,不会说英语,如有错误,请见谅。

【问题讨论】:

  • is_a 函数已被弃用很长时间(PHP 5.0)。请改用instanceof 运算符。
  • 我通过 php:codefunction random_str($length,$keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()-=_+[]{}\ \/|?;:\'\",.') { $str = ''; $max = mb_strlen($keyspace, '8bit') - 1; if ($max

标签: php mysql mysqli


【解决方案1】:

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

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

【讨论】:

  • 我一直认为最好将if($rsCaptchaID === false) 设置为$rsCaptchaID 可能看起来是错误的,因为它不是一个标量变量。但我想这只是个人选择
  • @RiggsFolly 我也倾向于在自己的代码中使用=== false 来建立良好的习惯。在这种情况下,虽然没有失败的可能性,因为mysqli_query 只能返回truefalse 或资源(php.net/manual/en/mysqli.query.php)。只有合法的错误才能到达die
  • 我的意思是,当它是一种资源时,它可能看起来像是假的!
  • 我不这么认为@RiggsFolly。具有属性的对象是真实的。即使是空对象,除非您的 PHP 已有十年历史。看到这个小提琴3v4l.org/7XTco。有关文档,请参阅Converting to boolean 部分:php.net/manual/en/language.types.boolean.php
  • 有趣的阅读
猜你喜欢
  • 2017-07-13
  • 2012-06-05
  • 1970-01-01
  • 2020-07-28
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多