We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    I’m converting a site to Revolution and I’ve got a really simple table that I need to run a query on.

    I’ve put it in the modx database and just want to duplicate what my old code does:

    $sql = "SELECT *
    FROM academic
    ORDER BY rand() LIMIT 25";
    
    $result = mysql_query($sql) or die (mysql_error()); 
    
    $count = mysql_num_rows($result);


    I tried working with XPDO and it took me a while to realize that it’s designed to interact with XPDO objects, (MODx objects in the context of Revolution). I tried using the DBAPI although it’s deprecated but still couldn’t get anything back but an empty array.

    I know I can do this the old-fashioned way with straight PHP, but since MODx already has a connection to the DB, it seemed that I should be able to take some shortcuts. So far, I haven’t found any but I’m probably missing something obvious.
      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
    • Quote from: BobRay at Sep 08, 2008, 05:24 AM

      $sql = "SELECT *
      FROM academic
      ORDER BY rand() LIMIT 25";
      
      $result = mysql_query($sql) or die (mysql_error()); 
      
      $count = mysql_num_rows($result);


      I tried working with XPDO and it took me a while to realize that it’s designed to interact with XPDO objects, (MODx objects in the context of Revolution). I tried using the DBAPI although it’s deprecated but still couldn’t get anything back but an empty array.

      I know I can do this the old-fashioned way with straight PHP, but since MODx already has a connection to the DB, it seemed that I should be able to take some shortcuts. So far, I haven’t found any but I’m probably missing something obvious.
      If you don’t want to create an xPDO model for your table, it’s just PDO, which every Revolution/xPDO developer should understand. MODx extends xPDO, which in turn is a wrapper for a PDO database connection. If you want to be able to take advantage of xPDO’s result-set caching, you can use xPDOCriteria:
      <?php
      $results= false;
      $count= 0;
      $query= new xPDOCriteria($modx, "SELECT * FROM `academic` ORDER BY rand() LIMIT 25");
      if ($query->prepare()) {
          if ($query->stmt->execute()) {
              if ($results= $query->stmt->fetchAll(PDO_FETCH_ASSOC)) {
                  $count= count($results);
              }
          }
      }
      ?>

      Even simpler if you don’t care about xPDO’s database result-set caching mechanism:
      <?php
      $rows= array();
      $stmt= $modx->query("SELECT * FROM `academic` ORDER BY rand() LIMIT 25");
      if ($stmt) {
          // loop through the result set and inspect one row at a time
          while ($row = $stmt->fetch(PDO_FETCH_ASSOC)) {
              array_push($rows, $row);
          }
      }
      ?>

        • 3749
        • 24,544 Posts
        Quote from: OpenGeek at Sep 08, 2008, 03:08 PM

        Even simpler if you don’t care about xPDO’s database result-set caching mechanism:
        <?php
        $rows= array();
        $stmt= $modx->query("SELECT * FROM `academic` ORDER BY rand() LIMIT 25");
        if ($stmt) {
              // loop through the result set and inspect one row at a time
            foreach ($stmt->fetch(PDO_FETCH_ASSOC) as $row) {
                array_push($rows, $row);
            
            }
        }
        ?>



        Thanks! This got me close, but it only returns one row (a different one each time). The foreach loop executes only once.

        [Update] This got me what I wanted:

        $rows = $stmt->fetchAll(PDO::FETCH_COLUMN, 1);
        grin
          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
        • Actually,
          foreach ($stmt->fetch(PDO_FETCH_ASSOC) as $row) {

          should have been
          while ($row = $stmt->fetch(PDO_FETCH_ASSOC)) {


          Sorry BobRay, that was my bad, and I caught myself doing it today as well...sigh. I corrected this in my original post.
          • Quote from: BobRay at Sep 08, 2008, 08:13 PM

            [Update] This got me what I wanted:

            $rows = $stmt->fetchAll(PDO::FETCH_COLUMN, 1);
            grin
            FYI, notice the difference in PDO::FETCH_COLUMN vs. PDO_FETCH_COLUMN. It’s fine and works either way, except PDO_FETCH_COLUMN is required if you want your Revolution component to work in PHP 4.
              • 37532
              • 1 Posts
              Thank you this is exactly what I've been looking for.
              I want to do some quick and dirty pages for prototyping, and the old dbapi totally didn't work.


              Thanks again
                • 38297
                • 2 Posts
                On PHP 5.2.17,
                while ($row = $stmt->fetch(PDO_FETCH_ASSOC)) {

                didn't work for me.
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

                did the job smiley
                  • 18985
                  • 29 Posts
                  This doesnt give any results for me, even when I query something that should return results.

                  <?php
                  $rows= array();
                  $stmt= $modx->query("SELECT * FROM `academic` ORDER BY rand() LIMIT 25");
                  if ($stmt) {
                      // loop through the result set and inspect one row at a time
                      while ($row = $stmt->fetch(PDO_FETCH_ASSOC)) {
                          array_push($rows, $row);
                      }
                  }
                  ?>
                  • These are old threads, use standard PDO constants, e.g. PDO::FETCH_ASSOC instead of PDO_FETCH_ASSOC. The latter is no longer supported by xPDO/modX.
                      • 18985
                      • 29 Posts
                      i actually did that, but thanks smiley, no differrence.

                      also, i noticed when i do this:
                      $sql =  "SELECT * FROM table o WHERE o.user_id = ? ORDER BY created DESC LIMIT 1";
                      
                      $stmt = $modx->prepare($sql);
                      $stmt->execute(array($user_id));
                      
                      echo $stmt->queryString;
                      


                      ..that the question mark is still in the query when i echo $stmt->queryString. Or is that suppose to happen?

                      EDIT:...and when $sql = 'SELECT * FROM table o WHERE o.user_id = '.$user_id.' ORDER BY created DESC LIMIT 1'

                      ..it gives me an error that the sql is invalid and that is because the variable has <code> tags around is, so the query atually reads:
                      SELECT * FROM table o WHERE o.user_id = <code class="php plain">2</code> ORDER BY created DESC LIMIT 1
                      [ed. note: meddiecap last edited this post 11 years, 10 months ago.]