简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM