We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48657
    • 17 Posts
    Hey Folks !

    I'm using the add-on "Login"
    Here's my problem

    [[!Profile]]
    [[+username]]
    

    its working, i get
    User1

    Now i have built a small sql query as php-snippet and named it getNumbers
    <?php
    $result = $modx->query("SELECT sum(numbers) FROM modx_test where user like '".$username."'");
    if (!is_object($result)) {
      return 'No result!';}
    else{
       $row = $result->fetchColumn();
       return  print_r($row,true);
    }


    which i' calling with
    [[getNumbers? &username=`[[+username]]`]]

    I dont get any print with this !

    i have checked if my parameter is correct. When i insert
    return $username
    at the end of my php snippet. its shown correctly.

    When i call the snippet with
    [[getNumbers? &username=`user1`]]

    i get the result i want to have

    i don't get it ,.... where is the mistake ?
    (hope you could follow this)

    This question has been answered by BobRay. See the first response.

      • 48657
      • 17 Posts
      when i dump my sql queryy it gets confusing

      [[getNumbers? &username=`[[+username]]`]]
      
      [[getNumbers? &username=`user1`]]
      


      lead to
      object(PDOStatement)#37 (1) { ["queryString"]=> string(70) "SELECT sum(numbers) FROM modx_test where user like 'user1'" } => Empty Reslt

      object(PDOStatement)#39 (1) { ["queryString"]=> string(65) "SELECT sum(numbers) FROM modx_test where user like 'user1'" } => 14.28
      [ed. note: compiler last edited this post 12 years, 1 month ago.]
        • 1343 ☆ A M B ☆
        • 2,213 Posts
        It might help some if you could explain the desired result. Is your goal to count the number of users with the same username, include a user ID with the username or what?
          Patrick | Server Wrangler
          About Me: Website | Tweets |  MODX Hosting
          • 48657
          • 17 Posts
          so lets say i have a table modx_test with columns: id(pr key), username (var char), numbers (double pr.)
          The strange thing here is that i obviously get 2 identicly sql queries that deliver different results.

          It strikes me that

          <?php
          $result = $modx->query("SELECT sum(numbers) FROM modx_test where user like '".$username."'");
          if (!is_object($result)) {
            return 'No result!';}
          else{
             $row = $result->fetchColumn();
             dump ($result);
          }


          Gives me 2 matching strings but it says that they have different charachter counts.

          Compare
          object(PDOStatement)#37 (1) { ["queryString"]=> string(70) "SELECT sum(numbers) FROM modx_test where user like 'user1'" }
          to
          object(PDOStatement)#37 (1) { ["queryString"]=> string(65) "SELECT sum(numbers) FROM modx_test where user like 'user1'" }


          It seems like, this variable is not transfered in php yet ... I guess the REAL query it is doing, is
          "SELECT sum(numbers) FROM modx_test where user like '[[+username]]'"
          which surely doesnt deliver results
            • 3749
            • 24,544 Posts
            What version of MODX are you using.

            For Evolution, I believe that should be $modx->db->query(), though I doubt if that's your problem.

            Also, in Evolution, when a query fails, MODX calls messageQuit(), which hopefully prints a message, then exits, so there's no return at all. That means can't test the return value and do anything in response.

            You might also just do SELECT username rather than SELECT sum(numbers), and just get a count of the resulting rows.

            Be aware that if 'user1' is the search key, it will never match 'user' in your LIKE query, but 'user' will match 'user1'. [ed. note: BobRay last edited this post 12 years, 1 month ago.]
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 48657
              • 17 Posts
              thanks for advices bob,
              I'm running modx revolution 2.3.1
              I'm pretty sure now that the sql query arriving on my sql server is
              "SELECT sum(numbers) FROM modx_test where user like '[[+username]]'"

              So it has not converted [[+username]] -> 'user1' , that must be the problem
              • discuss.answer
                • 3749
                • 24,544 Posts
                In your snippet, this will get the username of the current, logged in user:

                $username = $modx->user->get('username');


                The +username placeholder is no good unless some snippet sets its value with setPlaceholder(), and even then, it's no good in a snippet unless you get its value with getPlaceholder() or send the value in a snippet property. You can't use MODX tags directly in a snippet. PHP doesn't know how to parse them.
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 48657
                  • 17 Posts
                  that solved my problems. I thought it would have been parsed before the php procedure is called. But now i got it, thanx
                    • 3749
                    • 24,544 Posts
                    Glad I could help. smiley
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting