0

How do I get the values of the stdClass Object into a string variable so that I can take that variable and use it in an insert statement in another function. I'm using json_encode/json_decoding if that helps or makes a difference?

so the string variable will be:

$stdClassObjectStringVariable = 'test','[email protected]', 2, 15, 'test', '[email protected]', 'test test', 2, 14, 1, '','Lorem ipsom dolor..';

Object and Function:

stdClass Object
(
    [NominatorsName] => test
    [NominatorsEmail] => [email protected]
    [NomDivision] => 2
    [NomUnit] => 15
    [NominatorsSupervisor] => test
    [NominatorsSupervisorsEmail] => [email protected]
    [RecipientsName] => test test
    [RecipDivision] => 2
    [RecipUnit] => 14
    [Category] => 1
    [Reason] => 
    [Message] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    [Continue] => Continue...
)

public function SaveToDatabase(){
    $sql = "INSERT INTO recognition (
                `nominator`,
                `nominators_email`,
                `nominators_division_id`,
                `nomimators_unit_id`,
                `nominators_supervisor`,
                `nominators_supervisors_email`,
                `recipient`,
                `recipients_division_id`,
                `recipients_unit_id`,
                `category_id`,
                `reason`,
                `message`) VALUES(" . $stdClassObjectStringVariable . ");";
    $this->query($sql);
    return $this->insert_id;
}
9
  • stackoverflow.com/questions/2469222/… want this? Commented Jul 28, 2015 at 21:49
  • @stachu That doesn't produce something that can be substituted into an INSERT statement. Commented Jul 28, 2015 at 21:50
  • I saw that but I'm using json_encode and json_decode will it still work the same with that? Commented Jul 28, 2015 at 21:51
  • No, you don't want JSON. That's for sending to Javascript. Commented Jul 28, 2015 at 21:52
  • Of course not. I give you fishbot, if you want a code written by me so pay me :) Commented Jul 28, 2015 at 21:52

1 Answer 1

1

Maybe I am not understanding your question correctly but it seems to me that you want to get all of the properties form a class and format them into a string. if this is correct then here is an implementation.

Solution (but it depends upon the class property names matching the table column names)

I recommend using get_object_vars(). This will return an array of all accessible and non-static properties.

Then just implode() the array's values and use that for your string.

So maybe something like this...

$stringForQuery = implode(",",get_object_vars($someClass));

This does not guarantee the correct order however (Thanks @RiggsFolly).

DANGER: This is not necesarily safe for MySQL. This will NOT cleanse the data for MySQL or defend against MySQL injections. BE WARNED

More Comprehensive Solution

This solution solves some order and MySQL safety issues.

$properties = get_object_vars($someClass);

foreach ($properties as $key => $value)
    $cleanProperties[mysqli_real_escape_string($mysqliLink,$key)] = mysqli_real_escape_string($mysqliLink,$value);

$sql = "INSERT INTO recognition ('" .
    implode("','",array_keys($cleanProperties)) .
    "') VALUES ('" .
    implode("','",$cleanProperties)) .
    "');"
;
Sign up to request clarification or add additional context in comments.

2 Comments

And there is no guarantee that the data will be in the same order as the column names either.
That was basically what I was going to suggest, but only if she answered my question in the affirmative, that she could change the property names used in the class. Currently the property names dont match the column names so while I think this is the correct idea it does depend upon the class property names matching the table column names. And currently they dont!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.