我无法解决此错误在null上调用成员函数quote()

时间:2020-05-25 12:02:41

标签: php mysql database web

我正在对本地主机上的网站进行编程,当我尝试登录以提交新用户时发现此错误

致命错误:未捕获错误:调用成员函数quote()为null 在C:\ xampp \ htdocs \ recipes \ db_manager \ users.php:31堆栈跟踪中:#0 C:\ xampp \ htdocs \ recipes \ users_manager \ checkSubmit.php(15): notExistsUser('usertest')#1 {main}被抛出 第31行的C:\ xampp \ htdocs \ recipes \ db_manager \ users.php

checkSubmit.php:

<?php
    session_start();
    include_once("../db_manager/common.php");
    if(isset($_POST["username"]) && isset($_POST["pwd"])) {
        $username = $_POST["username"];
        $pwd = $_POST["pwd"];
        if(notExistsUser($username)){
            submit($username, $pwd);
            redirect("../index.php", "User successfully submitted!");
        } else {
            redirect("../submit.php", "This username already exists");
        }
    }
?>
users.php中的

funcion notExistsUser

function notExistsUser($username) {
    $db = attachdb();
    $user = $db->quote($username); //Line 31
    $rows = $db->query("SELECT * FROM Users WHERE username = $user");
    if($rows->rowCount() > 0)
        return false;
    else
        return true;
}

功能attachdb()

function attachdb() {
    $add = 'mysql:dbname=Recipes;host=localhost';
    try {
        $db = new PDO($add, 'root', 'mysql');
        return $db;
    } catch (PDOException $ex) {
        ?>
        <p>A database error occurred!.</p>
        <?php
        return NULL;
    }
}

此错误是什么意思,我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的数据库连接一定是错误的。您确定root的密码是mysql吗? 对于连接问题,attachdb()将返回NULL。

// at this point $db IS NULL and not the desired PDO Instance
$user = $db->quote($username); //Line 31

要解决此问题:您必须确保数据库连接正常,然后没有错误。