eldan88 Posted October 25, 2013 Share Posted October 25, 2013 Hey Guys. Before I start I accidentally spelled "composition" incorrectly. I'm not sure on how I can edit the title... or delete this post. Anyhow I have been trying to run a mysql insert query, but I am not getting any luck. Every time I run the index where I preform the command i get the following error "Catchable fatal error: Object of class DatabaseObject could not be converted to string in /Applications/XAMPP/xamppfiles/htdocs/photo_gallery/includes/database_object.php on line 77" I have put a comment on where that line number is that is on the database_object.php file below. But first I would like to show you the index file on where I am running the query. $dbobject = new DatabaseObject(); $user = new User($dbobject); $user ->username = "eldan2488"; $user->password = "abcd1234"; $user->first_name = "Eldan"; $user->last_name = "S"; $create = $user->create("users"); Here is my users class where I am calling the create method from the database_object Class User { private $db; public $id; public $username; public $password; public $first_name; public $last_name; public function create($table_name) { $attributes = $this->attributes(); return $this->db->create($table_name, $attributes); } // Gets the list of attributes keys and values public function attributes() { return get_object_vars($this); } } And here is the database_object class where it generating the error message from. class DatabaseObject { public function create($table_name, $attributes) { global $database; $sql = "INSERT INTO {$table_name} ( "; $sql .= join(", ",array_keys($attributes)); $sql .= ") VALUES ( '"; $sql .= join(", ", array_values($attributes)); // Line # 77 where error message is $query = $database->query($sql); } } Link to comment https://forums.phpfreaks.com/topic/283255-help-running-a-db-query-using-compistion/ Share on other sites More sharing options...
requinix Posted October 25, 2013 Share Posted October 25, 2013 public function attributes() { return get_object_vars($this); }is also going to include the private $db you have defined in the class. You don't want it. public function attributes() { return array( "id" => $this->id, "username" => $this->username, "password" => $this->password, "first_name" => $this->first_name, "last_name" => $this->last_name ); } Link to comment https://forums.phpfreaks.com/topic/283255-help-running-a-db-query-using-compistion/#findComment-1455326 Share on other sites More sharing options...
eldan88 Posted October 25, 2013 Author Share Posted October 25, 2013 Requinix! Thank you! That did the job! Link to comment https://forums.phpfreaks.com/topic/283255-help-running-a-db-query-using-compistion/#findComment-1455478 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.