I've got what I assume is a newbie question regarding variable interpolation. I have some code along the lines of:

function LoadDropdownItems()
{
  $DBPrimaryKey=1; 
  // Actually it's set elsewhere, but that's not the issue

  $LayoutQuery=mysql_query($LayoutQueryStatement);
  $Layout=mysql_fetch_array($LayoutQuery);
  $DropdownQueryStatement=$Layout['DropdownQuery'];
  $DropdownQuery=mysql_query($DropdownQueryStatement);
  // ...
}

Basically, I have a query statement stored in a database field, and I'm then running that query. It all works fine, except that in some cases I want to include PHP variables (such as $DBPrimaryKey) in the query statement and have them interpolated, and I can't get this to work. For example, if I have:

select DBPageID, Caption from dbpages where dbareaid=$DBPrimaryKey order by Caption;

in the database, that's what $DropdownQueryStatement gets set to. However, if I do:

$DropdownQueryStatement="select DBPageID, Caption from dbpages where dbareaid=$DBPrimaryKey order by Caption;"

it gets set to

select DBPageID, Caption from dbpages where dbareaid=1 order by Caption;

as you would expect. I've tried putting double-quotes around $Layout['DropdownQuery'], and tried putting them in the database itself, but neither of those work.

Putting it more simply, suppose I have:

$a='stuff';
$b='some $a';
$c=$b;

How do I change the third line such that the contents of $b will be interpolated and $c will wind up containing "some stuff"?

Any help would be appreciated.

    If I were going to concatenate a string to a varable I would do this:

    $B = "some text".$A;
    

    when I use a text varible whithin a string I do this:

    $SQL = "SELECT * FROM users WHERE name='$user'";
    

      Basically, I have a query statement stored in a database field, and I'm then running that query. It all works fine, except that in some cases I want to include PHP variables (such as $DBPrimaryKey) in the query statement and have them interpolated, and I can't get this to work.

      That's because variable interpolation only works on PHP string literals delimited by double quotes or in heredoc syntax. Here you have a string stored in the database, so having variable interpolation is potentially harmful as it could lead to corruption of data.

      I suggest going by another route entirely. Instead of trying to use PHP variable interpolation, use placeholders in prepared statement syntax, e.g., for the PDO extension. As such, you might store:

      select DBPageID, Caption from dbpages where dbareaid=:DBPrimaryKey order by Caption;

      Now, in your PHP code, prepare the statement with $DBPrimaryKey.

        You could use [man]eval/man:

        $a='stuff';
        $b='some $a';
        eval('$c="'.$b.'";');
        

        However, this gets a bit ugly and more difficult to maintain, plus if you are dealing with text from an external source (e.g. a database) to be eval()'d, then it also starts to become a security risk. I would therefore second Laselight's suggestion that you go with a place-holder replacement solution:

        select DBPageID, Caption from dbpages where dbareaid=[[DBPrimaryKey]] order by Caption
        
        $DropdownQueryStatement=str_replace('[[DBPrimaryKey]]', $DBPrimaryKey, $Layout['DropdownQuery']);
        
          Write a Reply...