PsyTrance Posted May 9, 2010 Share Posted May 9, 2010 Hello. I am making a website and I have written an administrating system. However, in order to get to the administrator panel you need to login with your admin credentials. I've written three different classes, one for handling the database using MySQLi, one for handling Users (cause I will add a membership system later) and one for initializing the database driver the user prefers. However, when I submit the login credentials to the page I get this error: Fatal error: Call to a member function fetch_assoc() on a non-object in C:\xampp\htdocs\database_mysqli.php on line 36 Here's the source code of all these classes: User.php: <?php // User Handler class class User { private $data; public function __construct($name) { if(isset($name)) $this->data = Database::getInstance()->fetchRow("SELECT `id`, `name`, `password`, `email` FROM `admins` WHERE `name`=".$name.";"); } public function __destruct(){ } public static function create($name, $password, $email) { $db = Database::getInstance(); $db->query("INSERT INTO `admins` (`name`, `password`, `email`) VALUES ('".$name."', '".$password."', '".$email."');"); return self::isOnline($name, $password); } public static function isOnline($name, $password) { if(isset($name)) return ($row = Database::getInstance()->fetchRow("SELECT `password` FROM `admins` WHERE `name`=".$name.";")) ? $row['password'] === $password : false; return false; } public function update($field, $value) { Database::getInstance()->query("UPDATE `admins` SET `".$field."` = '".$value."' WHERE `name`='".$this->data['name']."';"); $this->data[$field] = $value; } public function exists() { return isset($this->data['name']); } public function getPassword() { return $this->data['password']; } public function setPassword($newPass) { $this->update('password', Database::getInstance()->escapeString($newPass)); } public function getEmail() { return $this->data['email']; } public function setEmail($newEmail) { $this->update('email', Database::getInstance()->escapeString($newEmail)); } public function getId() { return $this->data['id']; } } ?> Database.php: <?php class Database { public function getInstance() { static $instance; if(!isset($instance)) { require_once("database_mysqli.php"); $instance = new Database_MySQLi(); return $instance; } return $instance; } public function fetchRow($query) { $result = $this->query($query); $row = $this->fetch($result); $this->freeResult($result); return $row; } public function fetchRows($result) { $this->fetch($result); } } ?> Database_MySQLi.php: <?php //Database handler class Database_MySQLi extends Database { private static $mysql; public function __construct() { global $database; $this->mysql = @new mysqli($database['host'], $database['user'], $database['pass'], $database['db']); if(mysqli_connect_errno()) { echo '<div class="error"><b>Connection Error:</b> '. mysqli_connect_error() . '</div>'; unset($this->mysql); return; } } public function __destruct() { $this->mysql->close(); } public function query($string) { try{ $result = $this->mysql->query($string); } catch(Exception $ex) { echo '<div class="error">Error: '. mysqli_error($this->mysql) .'</div>'; exit(); } return $result; } public function fetch($result) { return $result->fetch_assoc(); } public function freeResult($result) { return $result->close(); } public function escapeString($string) { return '\''. $this->mysql->real_escape_string($string) . '\''; } } ?> Can you possibly help me spot the error? Thanks in advance. Yours, PsyTrance. Quote Link to comment https://forums.phpfreaks.com/topic/201191-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object-in-dir/ Share on other sites More sharing options...
Alex Posted May 9, 2010 Share Posted May 9, 2010 You haven't provided enough information. The problem isn't in the code you provided, that's just where it's occurring because you provided the Database_MySQLi::fetch method with a variable that is expected to be an object but is not. To help you will need to post the code around where Database_MySQLi::fetch is being called. Quote Link to comment https://forums.phpfreaks.com/topic/201191-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object-in-dir/#findComment-1055537 Share on other sites More sharing options...
PsyTrance Posted May 9, 2010 Author Share Posted May 9, 2010 You haven't provided enough information. The problem isn't in the code you provided, that's just where it's occurring because you provided the Database_MySQLi::fetch method with a variable that is expected to be an object but is not. To help you will need to post the code around where Database_MySQLi::fetch is being called. On User.php on "isOnline" function. Here's the login page source code: <?php include("ui.php"); $ui = new UserInterface("Login Panel"); $do = $_GET['do']; if(!isset($_SESSION['id']) && !isset($_SESSION['password'])) { ?> <h2><font color="green"><u><strong>Admin Panel Login</strong></u></font></h2><br /> <form method="post" action="login.php?do=login" onsubmit="return checkForm()"> <script type="text/javascript"> function checkForm() { var user = document.getElementById('user'); var pass = document.getElementById('pass'); if (user.value == "" || pass.value == "") { var span = document.getElementById('err'); span.innerHTML = "<br /><div class='error'>You must enter all your credentials.</div>"; return false; } return true; } </script> <table cellpadding="0" cellspacing="1"> <tr> <td>Username:</td> <td class="TableRight"><input type="text" id="user" name="user" /></td> </tr> <tr> <td>Password:</td> <td class="TableRight"><input type="password" id="pass" name="pass" /></td> </tr> <tr> <td> </td> <td class="TableRight"><input type="submit" value="Login" /><input type="reset" value="Clear" /></td> </tr> </table> </form> <span id="err"></span> <?php } else { echo "Online!"; } if($do == "login") { $usr = $_POST['user']; $pass = $_POST['pass']; if(User::isOnline($usr, $pass)) { $_SESSION['user'] = $usr; $_SESSION['pass'] = $pass; header ("Location: login.php"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/201191-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object-in-dir/#findComment-1055549 Share on other sites More sharing options...
PsyTrance Posted May 11, 2010 Author Share Posted May 11, 2010 Bump. Can someone help me please? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/201191-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object-in-dir/#findComment-1056358 Share on other sites More sharing options...
trq Posted May 11, 2010 Share Posted May 11, 2010 The code is absolutely terrible I'm sorry to say. Not very well thought out and pretty hard to follow. One would assume that $result is not the object your expecting it to be. public function fetch($result) { return $result->fetch_assoc(); } Quote Link to comment https://forums.phpfreaks.com/topic/201191-fatal-error-call-to-a-member-function-fetch_assoc-on-a-non-object-in-dir/#findComment-1056372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.