【问题标题】:Fatal error: Uncaught Error: Call to undefined method mysqli::error() [duplicate]致命错误:未捕获的错误:调用未定义的方法 mysqli::error() [重复]
【发布时间】:2017-12-26 03:01:37
【问题描述】:

我正在尝试与 XAMPP 和 sql server 建立简单的连接。但是当我尝试输入数据或连接到数据库时,我得到这个错误。错误是什么?我的项目非常需要它。我不知道为什么php第9行是错误的。

致命错误:未捕获错误:调用 C:\xampp\htdocs\last1\new\register.php:19 中未定义的方法 mysqli::error()

    <?php
    /* Registration process, inserts user info into the database 
       and sends account confirmation email message
     */

    // Set session variables to be used on profile.php page
    $_SESSION['email'] = $_POST['email'];
    $_SESSION['first_name'] = $_POST['firstname'];
    $_SESSION['last_name'] = $_POST['lastname'];

    // Escape all $_POST variables to protect against SQL injections
    $first_name = $mysqli->escape_string($_POST['firstname']);
    $last_name = $mysqli->escape_string($_POST['lastname']);
    $email = $mysqli->escape_string($_POST['email']);
    $password = $mysqli->escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));
    $hash = $mysqli->escape_string( md5( rand(0,1000) ) );

    // Check if user with that email already exists
    **$result = $mysqli->query("SELECT * FROM users WHERE email='$email'") or die($mysqli->error());**

    // We know user email exists if the rows returned are more than 0
    if ( $result->num_rows > 0 ) {

        $_SESSION['message'] = 'User with this email already exists!';
        header("location: error.php");

    }
    else { // Email doesn't already exist in a database, proceed...

        // active is 0 by DEFAULT (no need to include it here)
        $sql = "INSERT INTO users (first_name, last_name, email, password, hash) " 
                . "VALUES ('$first_name','$last_name','$email','$password', '$hash')";

        // Add user to the database
        if ( $mysqli->query($sql) ){

            $_SESSION['active'] = 0; //0 until user activates their account with verify.php
            $_SESSION['logged_in'] = true; // So we know the user has logged in
            $_SESSION['message'] =

                     "Confirmation link has been sent to $email, please verify
                     your account by clicking on the link in the message!";

            // Send registration confirmation link (verify.php)
            $to      = $email;
            $subject = 'Account Verification ( clevertechie.com )';
            $message_body = '
            Hello '.$first_name.',

            Thank you for signing up!

            Please click this link to activate your account:

            http://localhost/login-system/verify.php?email='.$email.'&hash='.$hash;  

            mail( $to, $subject, $message_body );

            header("location: profile.php"); 

        }

        else {
            $_SESSION['message'] = 'Registration failed!';
            header("location: error.php");
        }

    }

请帮助我:(谢谢你们;*

【问题讨论】:

  • 您是否建立了与数据库的连接?
  • $mysqli 在哪里声明?它是否在另一个文件中,如果是,是否包含在内?你的代码中的双星号是为了突出一行,还是只是在这个问题中突出显示一行?
  • 我会把它发给你,你可以检查一下。你的邮箱是什么?
  • $mysqli 在 db.php 中声明。

标签: php mysql mysqli


【解决方案1】:

$mysqli-&gt;error() 应该是$mysqli-&gt;error,因为error 是一个属性,而不是一个方法。

【讨论】:

  • 我更改了它,但这就是它显示的“未选择数据库”:(
  • @G.Caps 好吧,你有没有连接到你的数据库服务器并选择数据库? secure.php.net/manual/en/mysqli.select-db.php
  • 您的电子邮件是什么,以便您查看?
【解决方案2】:

应该是

$mysqli-&gt;error

而不是$mysqli-&gt;error()

【讨论】:

  • 我更改了它,但这就是它显示的“未选择数据库”:(
【解决方案3】:

我认为你应该使用$mysqli-&gt;error 而不是$mysqli-&gt;error()

来源: http://php.net/manual/en/mysqli.error.php

【讨论】:

  • 我更改了它,但这就是它显示的“未选择数据库”:(
  • 嘿,我可以看到你声明 $mysqli 变量的代码
  • 如果你没有声明,那么它将不起作用,如果它是另一个文件,请使用include("mysqli.php");包含该文件
  • 你能帮帮我吗?请.. :(你的电子邮件是什么?我发给你
  • 你声明了 $mysqli 变量吗?我不能在这里发电子邮件,因为我是新来的表单并且不太了解规则
猜你喜欢
  • 2012-08-10
  • 2021-04-17
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多