Jump to content

Help running a DB query using compistion


Go to solution Solved by requinix,

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

}
Edited by eldan88
  • Solution

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
	);
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.