We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40122
    • 330 Posts
    I have an SQL query that looks like:

    SELECT COUNT (*) FROM `table` WHERE x = $x;


    and I need to store its results in a PHP variable, but I just cannot figure it our with PDO.

    So far I have tried:

    $sql = "SELECT COUNT (*) FROM `table` WHERE x = $x";
    $results = $modx->query($sql);
    $count = mysql_fetch_array($results);
    echo $count;


    and:

    $sql = "SELECT COUNT (*) FROM `table` WHERE x = $x";
    $results = $modx->getCount($sql);
    echo $results;


    but to no luck.

    Does anyone know the right way of doing this?

    Many thanks!

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

    [ed. note: meltingdog last edited this post 12 years, 7 months ago.]
    • discuss.answer
      • 38857
      • 2 Posts
      Try this...

      $sql = "SELECT COUNT(*) as num FROM modx_users";
      $result = $modx->query($sql);

      if (!is_object($result)) {
      return 'No result!';
      }
      else {
      $row = $result->fetch(PDO::FETCH_ASSOC);
      $numResults = $row['num'];
      return 'Result:' . $numResults;
      }


        • 40122
        • 330 Posts
        Quote from: zimbolite at Feb 10, 2014, 08:15 PM
        Try this...

        $sql = "SELECT COUNT(*) as num FROM modx_users";
        $result = $modx->query($sql);

        if (!is_object($result)) {
        return 'No result!';
        }
        else {
        $row = $result->fetch(PDO::FETCH_ASSOC);
        $numResults = $row['num'];
        return 'Result:' . $numResults;
        }




        Thanks, but I just tried this actually. It throws a PHP error.
          • 38857
          • 2 Posts
          I've just tried it, and it worked for me. What is the PHP error you get?
            • 10487 MODX Staff
            • 1,535 Posts
            Or, for example:
            $c = $modx->newQuery('modUser');
            $c->where(array(
                'username:LIKE' => 'a%'
            ));
            echo $modx->getCount('modUser', $c);
            


            See here for the types of criteria you can use in the where method.
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 40122
              • 330 Posts
              Quote from: zimbolite at Feb 10, 2014, 08:25 PM
              I've just tried it, and it worked for me. What is the PHP error you get?

              For some reason $modx->query($sql); would not run. If I dumped $result I would get bool(false)
                • 3749
                • 24,544 Posts
                Since you querying a MODX table, Gary Nutting's method is much easier and more reliable.

                This may help as well: http://bobsguides.com/revolution-objects.html
                  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
                  • 40122
                  • 330 Posts
                  Quote from: BobRay at Feb 11, 2014, 03:25 PM
                  Since you querying a MODX table, Gary Nutting's method is much easier and more reliable.

                  This may help as well: http://bobsguides.com/revolution-objects.html

                  Sorry - I should have clarified. I am queirying my own custom table (same DB). Can you use Gary's method for non-ModX related tables?

                  I have found a way to do it though:


                  $results = $modx->query("select count(items) as myitems from customtable where id >= '1'");
                  
                  if (!is_object($results)) {
                  
                     return 'No result!';
                  }
                  else {
                  
                      $r = $results->fetch(PDO::FETCH_ASSOC);
                  
                      echo 'uids = '.$r['myitems'];
                  }
                    • 3749
                    • 24,544 Posts
                    Gary's method, and the ones described in the link I posted, will work with non-MODX tables, but only if you make them xPDO-capable first. There's a method for doing that here: http://bobsguides.com/custom-db-tables.html.

                    It's definitely worth doing if you will be doing a lot of custom code for interacting with the DB, otherwise maybe not since you've found a solution.
                      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