Jump to content

Help running a DB query using compistion


eldan88

Recommended Posts

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);
        
                  
              
          
      }

}

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
	);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.