Php 致命错误:“;对非对象调用成员函数prepare();

Php 致命错误:“;对非对象调用成员函数prepare();,php,mysqli,Php,Mysqli,当我运行登录时,会出现以下错误: 致命错误:在第30行的/home/lankaf5/public_html/admin/admin/functions.php中对非对象调用成员函数prepare() 我已经标记了它所指的第30行;有人能检查一下并告诉我出了什么问题吗?我尝试了所有可能的方法,但都没有找到答案 // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // C

当我运行登录时,会出现以下错误:

致命错误:在第30行的/home/lankaf5/public_html/admin/admin/functions.php中对非对象调用成员函数prepare()

我已经标记了它所指的第30行;有人能检查一下并告诉我出了什么问题吗?我尝试了所有可能的方法,但都没有找到答案

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);


 function login($email, $password, $conn) {;
    $sql ="SELECT id, username, password, salt FROM members WHERE email = ? LIMIT 1";
    // Using prepared statements means that SQL injection is not possible. 
    if ($stmt = $conn->prepare($sql)) { // line 30 that the error is referring to
        $stmt->bind_param('s', $email);  // Bind "$email" to parameter.
        $stmt->execute();    // Execute the prepared query.
        $stmt->store_result();

    // get variables from result.
    $stmt->bind_result($user_id, $username, $db_password, $salt);
    $stmt->fetch();

    // hash the password with the unique salt.
    $password = hash('sha512', $password . $salt);
    if ($stmt->num_rows == 1) {
        // If the user exists we check if the account is locked
        // from too many login attempts 

        if (checkbrute($user_id, $conn) == true) {
            // Account is locked 
            // Send an email to user saying their account is locked
            return false;
        } else {
首先进行此修复:

function login($email, $password, $conn) {; // <-- remove the semi-colon
这将强制连接为
mysqli
类,如果未设置,则会在更接近问题根源的地方抛出致命错误


您仍然需要找出失败的原因。我怀疑您的数据库凭据不正确,或者您调用此函数时根本没有设置连接?

请在函数顶部放置
var\u dump($conn);死亡并告诉我们它说了什么。首先,你在顶部有一个open if语句,然后是一个开头带有无用分号的函数。你如何调用该函数?@CoolGuy:建议的编辑(从标题中删除用户特定路径)是可以接受的,因为它不适用于具有相同问题的任何其他用户。通常在编辑时,我会将高度本地化的错误复制到问题主体中,并使标题更通用。别忘了,这里的问题对未来的读者很有用,不仅仅是OP(即使这一个可能是重复的)。还有@Benka,请参见上文。删除它并没有什么不同。当我没有按照您的方式进行更改时,它给了我一个错误,说可捕获致命错误:传递给login()的参数3必须是mysqli的一个实例,给定null,在第11行的/home/lankaf5/public_html/admin/admin/process_login.php中调用,在第27行的/home/lankaf5/public_html/admin/admin/functions.php中定义!这表明您的错误在哪里-它在第11行的process_login.php中。在那里修复问题,或者将代码编辑到原始问题中,并在完成后ping我。这个过程是调试的核心-它是一个问题的重复跟踪,直到发现问题的根源。数据库连接也没有错误,因为通过添加函数和更新函数可以完美地工作…非常感谢。。。。解决了的。。。此处的连接名称有误。。。。。打字错误非常感谢
function login($email, $password, mysqli $conn) {