gpt4 book ai didi

php - fatal error : Call to a member function query() on a non-object in . php 文件

转载 作者:行者123 更新时间:2023-11-29 03:41:39 24 4
gpt4 key购买 nike

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

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

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

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

“PHP fatal error :在第 24 行的 ../App/class.manageUsers.php 中的非对象上调用成员函数 query(),referer:../App/login.php”

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

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

class.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;
}

}

?>

类.数据库.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();
}
}

}
?>

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

最佳答案

据我所知,错误的直接原因是“链接”属性为空。看起来 dbmanager 类在连接到数据库时失败了。这将解释为什么无法调用查询方法。

关于php - fatal error : Call to a member function query() on a non-object in . php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13102714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com