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

    Have run this query in phpMyAdmin and it works fine:

    SELECT COUNT( * ) AS  `Rows` , diallednumber
    FROM logData
    GROUP BY diallednumber
    ORDER BY  `Rows` DESC 
    LIMIT 20
    


    And am trying to put it into a XPDO query as follows

            //Query to find most common dialled numbers
            $c = $modx->myDB->newQuery('LogData');
    	$c->select('COUNT( * ) AS  `Rows` , diallednumber');
    	$c->groupby('diallednumber');
    	$c->sortby('Rows', 'DESC');
    	$c->limit(1);
    
            //Total found results
            $total = $modx->myDB->getCount('LogData', $c);
            echo '<p>Total records found: ' . $total . '</p>';
    
            //Run query
    	$results = $modx->myDB->getCollection('LogData', $c);
    


    If I run this I will get:


    Total records found: 0

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 523800 bytes) in /home/sites/paradoxal.co.uk/public_html/nhs/core/xpdo/om/xpdoobject.class.php on line 1686

    If I take out

    $results = $modx->myDB->getCollection('LogData', $c);
    


    The errors goes away. Any ideas what I am doing wrong?

    Thanks in advance.
    • You are executing a query to get a very specific result set, not one that represents a collection of objects, so the object methods getCount and getCollection simply do not make sense here. This is where you simply build the query as you have and then prepare() and execute() the PDOStatement to work with the result set in whatever way you want.

      $stmt = $c->prepare();
      if ($stmt && $stmt->execute()) {
          $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
      } else {
          /* there was an error, handle it */
      }
        • 40541
        • 85 Posts
        Much appreciated, that worked great. Will remember that one for the future.

        I'm using paginiation in this snippet and the below is coming up as 0

        $total = $modx->myDB->getCount('LogData', $c);
        echo '<p>Total records found: ' . $total . '</p>';
        


        Pagination is not working I'm thinking because I'm not returning a figure for $total. Thought the answer might be that I need to place it with the prepare() execute() but I did not have any luck?

        Many thanks.