Mary
Mary

Reputation: 1

Assign the values of an stdClass Object into a string variable with php

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

Upvotes: 0

Views: 914

Answers (1)

Hurricane Development
Hurricane Development

Reputation: 2464

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)) .
    "');"
;

Upvotes: 1

Related Questions