By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,462 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,462 IT Pros & Developers. It's quick & easy.


P: 79

I just recently started to switch to OOP in PHP, but am getting confused on almost every step :(

Right now, my current issue is i have this function:

Expand|Select|Wrap|Line Numbers
  1.     public function create() {
  2.         global $database;
  3.         $attributes = $this->sanitized_attributes();
  5.         $sql = "INSERT INTO ".self::$table_name." (";
  6.         $sql .= join(", ", array_keys($attributes));
  7.         $sql .= ") VALUES ('";
  8.         $sql .= join("', '", array_values($attributes));
  9.         $sql .= "')";
  11.       if($database->query($sql)) {
  12.         $this->id = $database->insert_id();
  13.         return true;
  14.       } else {
  15.         return false;
  16.       }
  17.     }
I am using a another function (method) to grab all the attributes i have set for the database fields, but when i use the above code - it tells me the first and second values are not valid integers.

I know that maybe because its adding those '' (single quotes) there but i am not sure how i can make this function work without modifying anything outside of this function?

also, if possible could some one provide to me or point me to a place that explains about instantiate?

I am instantiating my array to objects from within the class but wanting to know if there is an easier method to do so my current code is below:

Expand|Select|Wrap|Line Numbers
  1.     private static function instantiate($record) {
  2.         $object = new self;
  4.         foreach($record as $attribute=>$value){
  5.           if($object->has_attribute($attribute)) {
  6.             $object->$attribute = $value;
  7.           }
  8.         }
  10.         return $object;
  11.     }
Thank you,
Apr 29 '10 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,639
could you post the full error message?

your code is confusing me (i.e. I donít have enough information to knock sense in the problem, it would be great, if you could elaborate it)
Apr 29 '10 #2

P: 79

The above Create() method is within my my class it submits data into the database.

The error i received is "Invalid Integer Value for column name userid" - PHP doesn't send that error MySQL does from query.

I need a better way to write the query, currently right now if you look at my above function Create(), it is joining them but also adding ' (single quotes) when it joins them.

The first db field is userid and that field needs an integer value but the join i am using above is adding those single quotes even to the integer value.

I want it to write the query as it does but ignore adding '' single quotes for integer values since that makes it invalid for the SQL.

P.S :- you can ignore the other function Instantiate (i'll ask about it later after i resolved this other issue).

Hope that makes better sense now.
Apr 29 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.