Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I store a string into a single field in mySQL that I can echo in PHP, that also declares variables?

I have a .php page that has variables, each associated with dozens of different search criteria. I thought it would be easiest to just jam all the variables into the database as one gigantic string.

Then I thought it would be even EASIER to jam the variables and their declarations with them as one giant string.

So what I mean is, instead of just this:

Stewie
101
Green

I would instead like to have this as one big blob in the database:

$user_name = 'Stewie';
$pants_size = '101';
$favorite_eggs = 'Green';

That way, I would be able to VERY easily capture and extract a large amount of user preference data without having to do much in the way of arrays. After all, I have to change the page often enough that altering arrays would be a headache.

If I could do this, I could simply and it would be able to simply redeclare all the variables all at once, and I wouldn't have to think about it any more.

The records don't need to be searchable, just extractable with the variable declaration (like $var='value'; with the quotemarks and all).

What I investigated so far is as follows:

  • A javascript option. Yes, I know it would be possible to do some elaborate stuff with javascript, but it is far more complicated than my "one big variable that I want to echo, but magically also declare variables in" method. If I do this, I may as well do arrays.
  • An array option. This involves implode / explode stuff in .php, and is what I fear I will have to wind up choosing before too much longer. However, I would rather do it the one-step method I envision instead, if possible.
  • A 'store each variable separately in its own field in the database' option. This is horrible, because the fields change (and are added to) often enough that it would drive me batty. I would far sooner do the array option, because it's just a few little user preferences.

Any help at all would be appreciated, even if it is to tell me, "This isn't possible. I recommend [XYZ]."

Thanks!

EDIT: dave e has a great solution below for retrieving such data, via eval. But I am still a bit stumped as to how to get that data into the database to begin with. Storing >100 variables and their declarations ($variable_name='value') with the same ease is a bit perplexing!

like image 501
Jerry Avatar asked Nov 04 '22 23:11

Jerry


2 Answers

Make an aray of options like this:

$option_array['user_name'] = 'Stewie';
$option_array['pants_size'] = '101';

Use serialize to store the data

$stored_string = serialize($option_array);

when you get back data from db unserialize it $option_araay = unserialize($option_array);

!

like image 158
Stewie Avatar answered Nov 12 '22 11:11

Stewie


Using a comment from http://www.php.net/manual/en/function.get-defined-vars.php#73608

Use this slightly adapted function

/**
* @desc   works out the variables in the current scope(from where function was 
*         called).  Returns a serialized version of an array with variable name
*         as key and vaiable value as value
* @param  $varList: variables returned by get_defined_vars() in desired scope.
*         $excludeList: variables to be excluded from the list.
* @return string
*/
    function get_refined_vars($varList, $excludeList) {
        $temp1 = array_values(array_diff(array_keys($varList), $excludeList));
        $temp2 = array();
        while (list($key, $value) = each($temp1)) {
            global $$value;
            $temp2[$value] = $$value;
        }
        return serialize($temp2);
    }

Then create the exclude list array

$excludeList = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', 'excludeList');

Define your variables

    $user_name = 'Stewie';
    $pants_size = '101';
    $favorite_eggs = 'Green';

Now get all your define variables into a serialzied string

    //get all variables defined in current scope
    $varList = get_defined_vars();
    // refine the list, serialized.
    $storeThis = get_refined_vars($varList, $excludeList);

Store the $storeThis value in your mysql table

When you get the string out of mysql...

    extract(unserialize($someStringFromDatabaseResult));

    echo $user_name;
    echo $pants_size;
    echo $favorite_eggs'
like image 41
Creativehavoc Avatar answered Nov 12 '22 11:11

Creativehavoc