2

我是 PHP 和 SQL 的新手,但我正在尝试为我的 webapp 创建一个登录脚本。出于某种原因,我被卡住了。当用户名或密码字段为空时,有警报,但当它们都填写正确或错误时,没有警报。

当我在浏览器(Chrome)中打开网站并检查页面时,我得到:

“POST ../App/auth.php 500(内部服务器错误)”

当我从服务器打开 error.log 文件时,我得到:

“PHP 致命错误:在第 24 行的 ../App/class.manageUsers.php 中调用非对象的成员函数 query(),引用者:../App/login.php”

class.manageUsers.php 中的第 24 行是:

$query = $this->link->query("SELECT * FROM users WHERE username = '$username' AND
password = '$password' LIMIT 1");

类.manageUsers.php:

<?php

include_once('class.database.php');

class ManageUsers{

    protected $link;

    function __construct(){
        $db_conn = new ManageDatabase;
        $this->link = $db_conn->connect();
        return $this->link;
    }
    function addUsers($username,$firstname,$lastname,$email,$password,$user_level){
        $query = $this->link->prepare("INSERT INTO users (username,firstname,lastname,email,password,user_level)
            VALUES (?,?,?,?,?,?)
        ");
        $values = array($username,$firstname,$lastname,$email,$password,$user_level);
        $query->execute($values);
        $rowCount = $query->rowCount();
        return $rowCount;
    }
    function loginUsers($username,$password){
        $query = $this->link->query("SELECT * FROM users WHERE username = '$username' AND password = '$password' LIMIT 1");
        $rowCount = $query->rowCount();
        return $rowCount;           
    }
    function listUsers($user_id = null){
        if(isset($user_id))
        {
        $query = $this->link->query("SELECT * FROM users WHERE id = '$user_id' LIMIT 1");
        }
        else
        {
        $query = $this->link->query("SELECT * FROM users ORDER BY id DESC");
        }
        $rowCount = $query->rowCount();
        if($rowCount >= 1)
        {
            $result = $query->fetchAll();
        }
        else
        {
            $result = 0;
        }
        return $result;
    }

    function editUsers($user_id,$param){
        foreach ($param as $key => $value)
        {
        $query = $this->link->query("UPDATE users SET $key = '$value' WHERE id = '$user_id' LIMIT 1");
        }
        $rowCount = $query->rowCount();
        return $rowCount;
    }

    function deleteUsers($user_id){
        $query = $this->link->query("DELETE FROM users WHERE id = '$user_id' LIMIT 1");
        $rowCount = $query->rowCount();
        return $rowCount;
    }

}

?>

类.database.php:

<?php

class ManageDatabase{

    protected $db_conn;
    protected $db_host = 'localhost';
    protected $db_name = 'jqueryMobile';
    protected $db_user = 'root';
    protected $db_pass = '';

    function connect(){
        try{
            $this->db_conn = new PDO("mySQL:host=$this->db_host;dbname=$this->db_name",$this->db_user,$this->db_pass);
            return $this->db_conn;
        }
        catch(PDOexception $e){
            return $e->getMessage();
        }
    }

}
?>

我希望有人能找到它似乎出错的地方。谢谢。

4

1 回答 1

0

据我所知,错误的直接原因是空的“链接”属性。连接到 db 时,似乎 dbmanager 类失败。这可以解释为什么不能调用查询方法。

于 2012-10-30T20:03:01.197 回答