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','test@test.com', 2, 15, 'test', 'test@test.com', 'test test', 2, 14, 1, '','Lorem ipsom dolor..';

Object and Function:

stdClass Object
(
    [NominatorsName] => test
    [NominatorsEmail] => test@test.com
    [NomDivision] => 2
    [NomUnit] => 15
    [NominatorsSupervisor] => test
    [NominatorsSupervisorsEmail] => test@test.com
    [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;
}
Mary
  • 1
  • 1
  • http://stackoverflow.com/questions/2469222/how-to-convert-object-into-string-in-php want this? – ventaquil Jul 28 '15 at 21:49
  • @stachu That doesn't produce something that can be substituted into an `INSERT` statement. – Barmar Jul 28 '15 at 21:50
  • I saw that but I'm using json_encode and json_decode will it still work the same with that? – Mary Jul 28 '15 at 21:51
  • No, you don't want JSON. That's for sending to Javascript. – Barmar Jul 28 '15 at 21:52
  • Of course not. I give you fishbot, if you want a code written by me so pay me :) – ventaquil Jul 28 '15 at 21:52
  • Convert it to an array, described in http://stackoverflow.com/questions/2476876/how-do-i-convert-an-object-to-an-array. Then use `implode` to convert that to a comma-separated list. – Barmar Jul 28 '15 at 21:52
  • @Barmar Thanks! I'll give that a shot! – Mary Jul 28 '15 at 21:53
  • If you're using `json_decode` to get the stdClass then you can just use the second parameter of `json_decode` which will give you an associative array instead. Make sure you sanitise your data! – Scopey Jul 28 '15 at 21:55
  • Do you have control over the names of the properties used in the stdClass object? i.e. can you name the properties the same as the tables column names? – RiggsFolly Jul 28 '15 at 22:13

1 Answers1

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)) .
    "');"
;
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Hurricane Development
  • 2,449
  • 1
  • 19
  • 40
  • 2
    And there is no guarantee that the data will be in the same order as the column names either. – RiggsFolly Jul 28 '15 at 22:40
  • 1
    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! – RiggsFolly Jul 29 '15 at 09:23