【问题标题】:Notice: Trying to access array offset on value of type bool in line XX注意:尝试访问第 XX 行中 bool 类型值的数组偏移量
【发布时间】:2020-06-18 17:24:01
【问题描述】:

第 88 行尝试访问 bool 类型值的数组偏移量

尝试访问第 91 行中 bool 类型值的数组偏移量

我标记了下面的行,我试图更改查询以防万一,但它似乎不起作用。我查了一下,只是找不到解决方案。我还注意到所有这些答案都被否决了,但我是 PHP 的新手,所以我真的不知道要寻找什么。我想这很简单,但我只是没有找对地方。为什么会这样?我该如何解决?

$number = rand(pow(10, 11-1), pow(10, 11)-1);
$stmt = mysqli_prepare($db, "SELECT * FROM accounts WHERE email= ? OR number= ? LIMIT 1");
mysqli_stmt_bind_param($stmt, "ss", $email, $number);
mysqli_stmt_execute($stmt);

$user = mysqli_stmt_fetch($stmt);

function uniqueNum() {
    global $number;
    $number = rand(pow(10, 11-1), pow(10, 11)-1);
    if ($user['number'] === $number) {
        uniqueNum();
    }
}

if ($user) {
    if ($user['email'] === $email) { // Line 88
        array_push($errors, "E-Mail already exists");
    }
    if ($user['number'] === $number) { // Line 91
        uniqueNum();
    }
}

感谢任何形式的帮助!我今年 15 岁,正在学习如何填写注册表单,所以我很乐意听取一些额外的建议!

【问题讨论】:

  • 我假设// Line 81 的意思是// Line 88 ?
  • 天哪,对不起!是的,81 号线是 88 号线!我会尽力解决这个问题。
  • 您还应该在函数内收到$user 超出范围的错误
  • global $user; 会解决这个问题吗?
  • 函数 mysqli_stmt_fetch 返回一个布尔值,而不是行。

标签: php mysqli


【解决方案1】:

您将 PDO 与 mysqli 混淆了。 Mysqli 要复杂得多,mysqli_stmt_fetch 实际上并不返回任何值。如果您使用 get_result 和 OOP 样式,则可以使这变得更简单。然后,您可以使用 fetch_assoc() 从结果对象中获取单行。

$stmt = $db->prepare("SELECT * FROM accounts WHERE email= ? OR number= ? LIMIT 1");
$stmt->bind_param("ss", $email, $number);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();

if($user) {
    if ($user['email'] === $email) {
        array_push($errors, "E-Mail already exists");
    }
    if ($user['number'] === $number) {
        uniqueNum();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2020-05-20
    • 2020-10-03
    • 2020-11-16
    • 2021-03-31
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多