We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18913
    • 654 Posts
    Hi,
    I’m new to MODx so the solution to this question may be laughably simple. However, I’ve looked around the forums and can’t seem to find the answer. Here goes :

    I have a template variable (called [*test_tv*]) whose value I would like to use in a database query. (The TV contains the name of the table within the database that is relevant for the page using the template.) So there is a call to a chunk like this
    {{db_call}}
    which calls a snippet in this way :
    [[test_db_call? &passed_table=’[*test_tv*]’ ]]

    If the snippet is just this
    <?php
    echo $passed_table;
    ?>
    then the correct table value is printed out.

    But if the snippet looks like this
    <?php
    $dbhost = ’localhost’;
    $dbuser = ’theuser’;
    $dbpass = ’thepwd’;
    $dbname = ’thedb’;
    $dbtable = $passed_table;
    echo $passed_table;
    echo $dbtable;
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
    mysql_select_db($dbname) or die(mysql_error());
    $dbrequest = ’SELECT * FROM ’.$dbtable;
    echo $dbrequest;
    $data = mysql_query($dbrequest) or die(mysql_error());
    [... remaining code omitted ...]

    then I get a SQL error arising from the query request being
    "SELECT * FROM [*test_tv*]"
    (as opposed to something like "SELECT * FROM ’testtable’" )

    In other words, the specification of the TV is what is being used in the database request
    rather than the value of the TV being passed.

    Can anyone point me to something describing what I am doing wrong, or point the error out?

    Thanks in advance for any assistance. And if I have overlooked something obvious or posted this in the
    wrong forum, my apologies for that.

    Regards,
    Matt

    PS Also, from searching the forums it seems there are more MODx-friendly ways of handling database
    requests. I’m still working on trying to understand that ...
      • 18913
      • 654 Posts
      FWIW, if I change the line
      $dbtable = $passed_table;
      to
      $dbtable = $modx->documentObject[’test_tv’][1];
      then it works.

      I also changed all the query code to its equivalent "$modx->db[...]" code. Certainly looks nicer...

      Anyway, I’d still be interested to know how to directly use the TV if anyone knows.

      Thanks for listening...
      Matt
        • 10487 MODX Staff
        • 1,535 Posts
        You’ve encountered one of the limitations of the document parser in 0.9.6/Evo. The snippet call gets processed before the TV value so trying to pass the TV in via the snippet call is not going to work - at least, not the way you currently have it implemented. (FYI, MODx Revolution has a completely rewritten parser which allows for the kind of nesting you want)

        The method you adopted (accessing the documentObject) is the best way to do this. If you wanted flexibility in allowing the TV name to be passed via the snippet call then I would suggest the following change ....

        In your snippet, add this line at the top:
        $passed_table = array_key_exists($passed_table, $modx->documentObject) ? $modx->documentObject[$passed_table][1] : 'test_tv'; /* adopt a default if invalid/no TV is passed */

        And then, change your snippet call to this:
        [[test_db_call? &passed_table=`test_tv`]]
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com
          • 18913
          • 654 Posts
          Thanks. And I appreciate the feedback.
          Matt