0

I can echo the current entry_id but when I try to pass that variable to a mySQL statement, it doesn't work.

{exp:channel:entries channel="test" entry_id="20"}
  <?php
  $entry_id = '{entry_id}';
  echo $entry_id; // returns 20

  $sql_query = "SELECT col1 FROM table WHERE entry_id = $entry_id"; // doesn't work
  $sql_query = "SELECT col1 FROM table WHERE entry_id = 20"; // this works

  // rest of code
  ?> 
{/exp:channel:entries} 
1

I don't think it would work with any of "PHP Parsing Stage" of template preference. You can use Query module tag instead of using PHP code with channel entry tag like:

{exp:channel:entries channel="test" entry_id="20"}

{exp:query sql="SELECT col1 FROM table WHERE entry_id = '{entry_id}'" parse="inward"}
{col1}
{/exp:query}

{/exp:channel:entries}

It would work for you.

* Edited * In the case, it's necessary for you to have PHP code within the template you can embed a template within it like:

    {exp:channel:entries channel="test" entry_id="20"}

{embed="temp_grp/embedded_temp" entry_id="{entry_id}"}

    {/exp:channel:entries}

and you can have PHP code within the template like:

  <?php
  $entry_id = "{embed:entry_id}";

  $sql_query = "SELECT col1 FROM table WHERE entry_id = $entry_id";

  // rest of code
  ?> 

Don't forget setting the "PHP Parsing Stage" as "Output" in template preferences with "Allow PHP"

4
  • Thanks, but is there any way of doing it without the query module?
    – Gaillimh
    May 16 '13 at 5:09
  • Yes, you can embed another template and put the PHP code within that and pass entry_id as embed variable like: {embed="temp_group/temp_name" entry_id="{entry_id}"} and you can use {embed:entry_id} within "temp_group/temp_name" and use $entry_id = '{embed:entry_id}'; with further coding. Do not forget setting the "PHP Parsing Stage" as "Output" in template preferences with "Allow PHP". May 16 '13 at 5:16
  • It worked! Thanks so much. Can you edit that into your answer? Regards.
    – Gaillimh
    May 16 '13 at 6:46
  • Glad, it worked for you. I edited my answer. May 16 '13 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.