0

Using PHP variable variables with mysqli_fetch_assoc to auto-format variables like $column_name="some_value" (code below):

while ($account_row=mysqli_fetch_assoc($account_results))
    {
    foreach ($account_row as $key=>$value)
        {
        $$key=trim(stripslashes($value));
        }
    }

So if I have a column "username" and row value "someuser", this code creates:

$username="someuser";

However, in some cases I need variable names to be DIFFERENT from column names. For example, I need code to create:

$username_temp="someuser";

How could I do that? Using this code gives an error:

$$key."_temp"=trim(stripslashes($value));

No other ideas in my head.

Mindaugas Li
  • 1,071
  • 5
  • 15
  • 37

2 Answers2

1

change $$key."_temp" to ${$key."_temp"} have a look on below solution:

$value = 'test';
$key="someuser";
${$key."_temp"}=$value;
echo $someuser_temp; //output test
Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
0

Please try this ${$key . "_temp"} = trim(stripslashes($value));

Jimmy Ko
  • 857
  • 1
  • 8
  • 20