spedax Posted July 28, 2009 Share Posted July 28, 2009 $sql = "SELECT * FROM users WHERE username LIKE '". $username . "' LIMIT 1"; $result_array = user::find_by_sql($sql); $user = array_shift($result_array); print_r($user); if ($user->username !== $username && $password == $password2 ) { when I print out $user I get this: User Object ( [id] => 5 [username] => test [password] => test [level] => 1 [email] => test@test.com ) I get an error on line 17 ( last line of code) error: notice: Trying to get property of non-object in C:\wamp\www\cms\pages\register.php on line 17 anyone got any idea what this could be? It says its not an object while it is one... any help is appriciated! addional code: user class <?php class User extends DatabaseObject { protected static $table_name="users"; protected static $db_fields = array('id', 'username', 'password', 'level', 'email', 'squad_id'); var $id; var $username; var $password; var $level; var $email; static function authenticate($username="", $password="") { global $database; $username = $database->escape_value($username); $password = $database->escape_value($password); $sql = "SELECT * FROM users "; $sql .= "WHERE username = '{$username}' "; $sql .= "AND password = '{$password}' "; $sql .= "LIMIT 1"; $result_array = self::find_by_sql($sql); return !empty($result_array) ? array_shift($result_array) : false; } function join_date() { $join_date = strftime("$d/$m/$y",$this->timestamp); return $join_date; } } ?> database object class <?php // If it's going to need the database, then it's // probably smart to require it before we start. require_once(LIB_PATH.DS.'database.php'); class DatabaseObject { public static function find_all() { return static::find_by_sql("SELECT * FROM ".static::$table_name); } public static function find_by_id($id=0) { $result_array = static::find_by_sql("SELECT * FROM ".static::$table_name." WHERE id={$id} LIMIT 1"); return !empty($result_array) ? array_shift($result_array) : false; } public static function find_by_sql($sql="") { global $database; $result_set = $database->query($sql); $object_array = array(); while ($row = $database->fetch_array($result_set)) { $object_array[] = static::instantiate($row); } return $object_array; } public static function count_all() { global $database; $sql = "SELECT COUNT(*) FROM ".static::$table_name; $result_set = $database->query($sql); $row = $database->fetch_array($result_set); return array_shift($row); } private static function instantiate($record) { $class_name = get_called_class(); $object = new $class_name; foreach($record as $attribute=>$value){ if($object->has_attribute($attribute)) { $object->$attribute = $value; } } return $object; } private function has_attribute($attribute) { // We don't care about the value, we just want to know if the key exists // Will return true or false return array_key_exists($attribute, $this->attributes()); } protected function attributes() { // return an array of attribute names and their values $attributes = array(); foreach(static::$db_fields as $field) { if(property_exists($this, $field)) { $attributes[$field] = $this->$field; } } return $attributes; } protected function sanitized_attributes() { global $database; $clean_attributes = array(); // sanitize the values before submitting // Note: does not alter the actual value of each attribute foreach($this->attributes() as $key => $value){ $clean_attributes[$key] = $database->escape_value($value); } return $clean_attributes; } public function save() { // A new record won't have an id yet. return isset($this->id) ? $this->update() : $this->create(); } public function create() { global $database; // Don't forget your SQL syntax and good habits: // - INSERT INTO table (key, key) VALUES ('value', 'value') // - single-quotes around all values // - escape all values to prevent SQL injection $attributes = $this->sanitized_attributes(); $sql = "INSERT INTO ".static::$table_name." ("; $sql .= join(", ", array_keys($attributes)); $sql .= ") VALUES ('"; $sql .= join("', '", array_values($attributes)); $sql .= "')"; if($database->query($sql)) { $this->id = $database->insert_id(); return true; } else { return false; } } public function update() { global $database; // Don't forget your SQL syntax and good habits: // - UPDATE table SET key='value', key='value' WHERE condition // - single-quotes around all values // - escape all values to prevent SQL injection $attributes = $this->sanitized_attributes(); $attribute_pairs = array(); foreach($attributes as $key => $value) { $attribute_pairs[] = "{$key}='{$value}'"; } $sql = "UPDATE ".static::$table_name." SET "; $sql .= join(", ", $attribute_pairs); $sql .= " WHERE id=". $database->escape_value($this->id); $database->query($sql); return ($database->affected_rows() == 1) ? true : false; } public function delete() { global $database; // Don't forget your SQL syntax and good habits: // - DELETE FROM table WHERE condition LIMIT 1 // - escape all values to prevent SQL injection // - use LIMIT 1 $sql = "DELETE FROM ".static::$table_name; $sql .= " WHERE id=". $database->escape_value($this->id); $sql .= " LIMIT 1"; $database->query($sql); return ($database->affected_rows() == 1) ? true : false; // NB: After deleting, the instance of User still // exists, even though the database entry does not. // This can be useful, as in: // echo $user->first_name . " was deleted"; // but, for example, we can't call $user->update() // after calling $user->delete(). } } database object <?php require_once(LIB_PATH.DS."config.php"); class MySQLDatabase { private $connection; public $last_query; private $magic_quotes_active; private $real_escape_string_exists; function __construct() { $this->open_connection(); $this->magic_quotes_active = get_magic_quotes_gpc(); $this->real_escape_string_exists = function_exists( "mysql_real_escape_string" ); } public function open_connection() { $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS); if (!$this->connection) { die("Database connection failed: " . mysql_error()); } else { $db_select = mysql_select_db(DB_NAME, $this->connection); if (!$db_select) { die("Database selection failed: " . mysql_error()); } } } public function close_connection() { if(isset($this->connection)) { mysql_close($this->connection); unset($this->connection); } } public function query($sql) { $this->last_query = $sql; $result = mysql_query($sql, $this->connection); $this->confirm_query($result); return $result; } public function escape_value( $value ) { if( $this->real_escape_string_exists ) { // PHP v4.3.0 or higher // undo any magic quote effects so mysql_real_escape_string can do the work if( $this->magic_quotes_active ) { $value = stripslashes( $value ); } $value = mysql_real_escape_string( $value ); } else { // before PHP v4.3.0 // if magic quotes aren't already on then add slashes manually if( !$this->magic_quotes_active ) { $value = addslashes( $value ); } // if magic quotes are active, then the slashes already exist } return $value; } // "database-neutral" methods public function fetch_array($result_set) { return mysql_fetch_array($result_set); } public function num_rows($result_set) { return mysql_num_rows($result_set); } public function insert_id() { // get the last id inserted over the current db connection return mysql_insert_id($this->connection); } public function affected_rows() { return mysql_affected_rows($this->connection); } private function confirm_query($result) { if (!$result) { $output = "Database query failed: " . mysql_error() . "<br /><br />"; $output .= "Last SQL query: " . $this->last_query; die( $output ); } } } $database = new MySQLDatabase(); $db =& $database; ?> Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/ Share on other sites More sharing options...
WolfRage Posted July 28, 2009 Share Posted July 28, 2009 Some things to look for would be where the object was initiated. That instance is only aviable with in the scope of which it was initialized. Thus if it was initialized in the main script it would not be aviable inside of a function or a class with out being passed into the function or class. http://www.php.net/manual/en/language.variables.scope.php Remember variable scope also applies to objects. Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885313 Share on other sites More sharing options...
spedax Posted July 28, 2009 Author Share Posted July 28, 2009 Im kinda confused, it it was a scope issue I would assume he wouldnt find the class or the variable, but he actually does. Could be im wrong thou, kinda a newbie with OOP, objects,scopes.... thanks for the fasy reply. Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885320 Share on other sites More sharing options...
WolfRage Posted July 28, 2009 Share Posted July 28, 2009 Hey what is that last line or better yet last few lines of code? Can you post register.php? Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885332 Share on other sites More sharing options...
spedax Posted July 28, 2009 Author Share Posted July 28, 2009 <?php // Remember to give your form's submit tag a name="submit" attribute! if (isset($_POST['submit'])) { // Form has been submitted. $username = trim($_POST['username']); $password = trim($_POST['password']); $password2 = trim($_POST['password2']); $email = trim($_POST['email']); // see if row already excist by first getting it out of the DB and then comparing it to the given username $sql = "SELECT * FROM users WHERE username LIKE '". $username . "' LIMIT 1"; $result_array = user::find_by_sql($sql); $user = array_shift($result_array); print_r($user); if ($user->username !== $username && $password == $password2 ) { // create new user if username doesnt excist and given passwords are equal $new_user = new User(); $new_user->username = $username; $new_user->password = $password; $new_user->email = $email; $new_user->level = 1; $new_user->create(); $new_user_profile = new UserProfile(); $new_user_profile->id = $new_user->id; $new_user_profile->active = 0; $new_user_profile->create(); $new_user_stats = new UserStats(); $new_user_stats->id = $new_user->id; $new_user_stats->timestamp_created = time(); $new_user_stats->create(); echo output_message("User has been created! <br/>"); echo output_message( '<a href="index.php?">Index</a>'); } if ($user->username == $username ) { echo output_message("username already excists!"); ?> <h2>register</h2> <form action="index.php?pg=register" method="post"> <table> <tr> <td>Username:</td> <td> <input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /> </td> </tr> <tr> <td>Password:</td> <td> <input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" /> </td> </tr> <tr> <td>Password again:</td> <td> <input type="password" name="password2" maxlength="30" value="<?php echo htmlentities($password2); ?>" /> </td> </tr> <tr> <tr> <td>Email:</td> <td> <input type="text" name="email" maxlength="30" value="<?php echo htmlentities($email); ?>" /> </td> </tr> <tr> <td colspan="2"> <input type="submit" name="submit" value="register" /> </td> </tr> </table> </form> <?php } elseif ($password != $password2) { echo output_message("Passwords are not equal!"); ?> <h2>register</h2> <form action="index.php?pg=register" method="post"> <table> <tr> <td>Username:</td> <td> <input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /> </td> </tr> <tr> <td>Password:</td> <td> <input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" /> </td> </tr> <tr> <td>Password again:</td> <td> <input type="password" name="password2" maxlength="30" value="<?php echo htmlentities($password2); ?>" /> </td> </tr> <tr> <tr> <td>Email:</td> <td> <input type="text" name="email" maxlength="30" value="<?php echo htmlentities($email); ?>" /> </td> </tr> <tr> <td colspan="2"> <input type="submit" name="submit" value="register" /> </td> </tr> </table> </form> <?php } } else { $username = ""; $password = ""; $password2 = ""; $email = ""; ?> <h2>register</h2> <form action="index.php?pg=register" method="post"> <table> <tr> <td>Username:</td> <td> <input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /> </td> </tr> <tr> <td>Password:</td> <td> <input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" /> </td> </tr> <tr> <td>Password again:</td> <td> <input type="password" name="password2" maxlength="30" value="<?php echo htmlentities($password2); ?>" /> </td> </tr> <tr> <td>Email:</td> <td> <input type="text" name="email" maxlength="30" value="<?php echo htmlentities($email); ?>" /> </td> </tr> <tr> <td colspan="2"> <input type="submit" name="submit" value="register" /> </td> </tr> </table> </form> <?php } ?> and that one is included to index <?php ini_set('display_errors',1); error_reporting(E_ALL); require_once("inc/initialize.php"); require_once("templates/dev/header.php"); ?> <div id="sideBar"> <div class="panel"> <h4>Sidebar</h4> </div> </div> <div id="sideBarright"> <div class="panel"> <h4>Sidebar</h4> </div> </div> <div id="content"> <?php if (isset($_GET[ACTION]) && $_GET[ACTION] != "") { $action = $_GET[ACTION]; if (file_exists('pages/'.$action.'.php')) { require_once('pages/'.$action.'.php'); } elseif (!file_exists('pages/' .$action. '.php')) { echo 'Page you are requesting doesn´t exist'; } } else { $test = $session->is_logged_in(); if ( $test != true){ echo '<a href="index.php?' . ACTION . '=login">Login</a> <br/>'; echo 'or <a href="index.php?' . ACTION . '=register">register</a>'; } else { echo "Your logged in <br />"; echo '<a href="index.php?' . ACTION . '=logout">Logout</a>'; } } ?> </div> <?php require_once("templates/dev/footer.php"); ?> and the classes are included here <?php // Define the core paths // Define them as absolute paths to make sure that require_once works as expected // DIRECTORY_SEPARATOR is a PHP pre-defined constant // (\ for Windows, / for Unix) defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR); defined('SITE_ROOT') ? null : define('SITE_ROOT','C:'.DS.'wamp'.DS.'www'.DS.'cms'); defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'inc'); // load config file first require_once(LIB_PATH.DS.'config.php'); // load basic functions next so that everything after can use them require_once(LIB_PATH.DS.'functions.php'); // load core objects require_once(LIB_PATH.DS.'session.php'); require_once(LIB_PATH.DS.'database.php'); require_once(LIB_PATH.DS.'database_object.php'); // load database-related classes //require_once(LIB_PATH.DS.'news.php'); require_once(LIB_PATH.DS.'user.php'); require_once(LIB_PATH.DS.'user_profile.php'); require_once(LIB_PATH.DS.'user_stats.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885342 Share on other sites More sharing options...
spedax Posted July 28, 2009 Author Share Posted July 28, 2009 the first file is register.php its in a map pages wich gets automaticly included. Thanks for getting a look into it, hope it makes sense to you cheers Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885343 Share on other sites More sharing options...
KevinM1 Posted July 28, 2009 Share Posted July 28, 2009 The method returns an array. Have you tried $user[0]->username ?? Also, there are several questionable things going on in general, including: 1. Mixing PHP 4 and PHP 5 OO syntax 2. User allowing direct public access to its properties 3. User being a child of DatabaseObject 4. Using array_key_exists() on an object, which is deprecated behavior as of 5.3. Using property_exists(), or, even better, the Reflection API is the way to go here. Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885346 Share on other sites More sharing options...
WolfRage Posted July 28, 2009 Share Posted July 28, 2009 Thanks Nightslyr I had just noticed that print_r() was returning an array and was going to have him do a var_dump on it to prove it as an array. Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885347 Share on other sites More sharing options...
spedax Posted July 28, 2009 Author Share Posted July 28, 2009 Fatal error: Cannot use object of type User as array in C:\wamp\www\cms\pages\register.php on line 16 code : if ($user[0]->username == $username ) mind if I send you a pm, I have some questions about the things you said. Thanks for the hints! Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885352 Share on other sites More sharing options...
spedax Posted July 28, 2009 Author Share Posted July 28, 2009 I tried creating a getter for the variable but still get an error ... Fatal error: Call to a member function get_username() on a non-object in Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885359 Share on other sites More sharing options...
WolfRage Posted July 28, 2009 Share Posted July 28, 2009 Shouldn't that be just $user[0] with out the ->username. <?php if ($user[0] == $username ) ?> Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885361 Share on other sites More sharing options...
spedax Posted July 28, 2009 Author Share Posted July 28, 2009 thanks, that actually seemed to work. No idea whats going on thou, going to look into it ( if I do the same thing on another class it does return as an object in an array ... very strange ) gone make this topic solved. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/167854-solved-trying-to-get-property-of-non-object-error/#findComment-885367 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.