【问题标题】:Fatal error: Call to a member function close() on a non-object in [duplicate]致命错误:在 [重复] 中调用非对象上的成员函数 close()
【发布时间】:2015-03-12 19:35:55
【问题描述】:

首先,请记住,我花了几个小时试图解决这个问题,所以请不要“判断”我发布的同一个问题已经回答了......我访问了这些在这里发帖:

Fatal error: Call to a member function close() on a non-object. MySQLi issue 和 http://w3guy.com/fatal-error-call-member-function-fetch_object-non-object/

但是这些对我不起作用,我不知道为什么......

我的代码:

<?php
include_once 'db_connect.php';
include_once 'psl-config.php';

$error_msg = "";

if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {
    // Sanitize and validate the data passed in
    $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Not a valid email
        $error_msg .= '<p class="error">The email address you entered is not valid</p>';
    }

    $password = filter_input(INPUT_POST, 'p', FILTER_SANITIZE_STRING);

    // Username validity and password validity have been checked client side.
    // This should should be adequate as nobody gains any advantage from
    // breaking these rules.
    //

    $prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
    $stmt = $mysqli->prepare($prep_stmt);

   // check existing email  
    if ($stmt) {
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows == 1) {
            // A user with this email address already exists
            $error_msg .= '<p class="error">A user with this email address already exists.</p>';
                        $stmt->close();
        }
                $stmt->close();
    } else {
        $error_msg .= '<p class="error">Database error Line 39</p>';
                $stmt->close();
    }

    // check existing username
    $prep_stmt = "SELECT id FROM members WHERE username = ? LIMIT 1";
    $stmt = $mysqli->prepare($prep_stmt);

    if ($stmt) {
        $stmt->bind_param('s', $username);
        $stmt->execute();
        $stmt->store_result();

                if ($stmt->num_rows == 1) {
                        // A user with this username already exists
                        $error_msg .= '<p class="error">A user with this username already exists</p>';
                        $stmt->close();
                }
                $stmt->close();
        } else {
                $error_msg .= '<p class="error">Database error line 55</p>';
                $stmt->close();
        }

    // TODO: 
    // We'll also have to account for the situation where the user doesn't have
    // rights to do registration, by checking what type of user is attempting to
    // perform the operation.

    if (empty($error_msg)) {

        // Create salted password 
        $passwordHash = password_hash($password, PASSWORD_BCRYPT);

        // Insert the new user into the database 
        if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password) VALUES (?, ?, ?)")) {
  $insert_stmt->bind_param('sss', $username, $email, $passwordHash);
            // Execute the prepared query.
            if (! $insert_stmt->execute()) {
                header('Location: ../error.php?err=Registration failure: INSERT');
            }
        }
        header('Location: ./continue.php');
    }
}

【问题讨论】:

  • 好的...当您收到此错误消息时,您会采取什么措施?
  • @bcesars - 注册
  • 检查 IF/ELSE 语句中的每个 $stmt-&gt;close();。您的代码中有一些逻辑错误。执行代码时需要一步一步检查。
  • 你有时会尝试 close() 两次。
  • 我得到了准确的 2 行这些错误发生的地方:else { $error_msg .= '

    Database error line 55

    '; $stmt->关闭();在这里: else { $error_msg .= '

    数据库错误第 39 行

    '; $stmt->关闭(); }

标签: php mysqli


【解决方案1】:
if ($stmt->num_rows == 1) {
     $error_msg .= '<p class="error">A user with this email address already exists.</p>';
     $stmt->close();
}
$stmt->close();

应该是

if ($stmt->num_rows == 1) {
    $error_msg .= '<p class="error">A user with this email address already exists.</p>';
    $stmt->close();
} else {
    $stmt->close();
}

否则在 num_rows == 1 的情况下关闭它两次。

或更好:

if ($stmt->num_rows == 1) {
    $error_msg .= '<p class="error">A user with this email address already exists.</p>';
}
$stmt->close();

另一个事实是: 在IF($stmt) 的ELSE 中,您不能执行$stmt-&gt;close();,因为$stmt 不存在。

【讨论】:

    猜你喜欢
    • 2012-04-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 2017-12-09
    相关资源
    最近更新 更多