We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2740
    • 95 Posts
    Hello, I’ve a simple database query that works in phpadmin but doesn’t find any results when in a snippet. The only difference between the two tests is that I replace $LTitle with the actual search term in the phpadmin test.

    The snippet checks if a tv in parent 204 has the same value as the current longtitle, and if it does it runs a ditto chunk. Snippet call:

    <?php
    $o = " ";
    $LTitle = $modx->documentObject['longtitle'];
    $result = $modx->db->query( 'SELECT value FROM `modx_site_tmplvar_contentvalues` LEFT JOIN modx_site_content ON modx_site_content.id = modx_site_tmplvar_contentvalues.contentid
    WHERE value = "$LTitle" AND parent = 204 LIMIT 1' );
    $total_rows = $modx->db->getRecordCount( $result );
    if( $total_rows == 1 ) {
    $o = "{{ditto_assoc_reviews}}"; 
    return $o; }
    else
    $o = "no results found";
    return $o;
    ?>


    Chunk ditto_assoc_reviews:

    <h3>Reviews</h3><ul>
    [!Ditto? &parents=`204` &tpl=`book_review_list` &localFilterDelimiter=`,,` &noResults=`No reviews` &filter=`tvbook_title,,[*longtitle*],,7` &orderBy=`longtitle ASC`!]
    </ul>


    What mistake have I made?
      • 37059
      • 368 Posts
      I am no PHP expert, but I think the problem might have something to do with single vs. double quotes and your $LTitle variable.

      When a variable appears inside double quotes, PHP parses it as a variable, replacing it with the variable’s value. When it appears inside single quotes, however, PHP leaves it unprocessed, considering it text.

      Although your $LTitle is in double quotes individually, it is contained in a string (your database query) that appears in single quotes.

      I would try enclosing the query in double quotes and see if that makes any difference.
        Jason
        • 28042 ☆ A M B ☆
        • 24,524 Posts
         'SELECT value FROM `modx_site_tmplvar_contentvalues` LEFT JOIN modx_site_content ON modx_site_content.id = modx_site_tmplvar_contentvalues.contentid
        WHERE value = "' . $LTitle . '" AND parent = 204 LIMIT 1'

        would also work; breaking the variable out of the single-quoted text and appending with the . operator.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 2740
          • 95 Posts
          Thank you kenquad and sottwell for your help, both solutions worked. I really need to brush up on the basics of php!