We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40122
    • 330 Posts
    Hi all,

    I am having a good deal of trouble connecting to the database via a snippet in Revo.

    I have the simple code:

    $leadersql = "SELECT * FROM `mytable`";
    
    $result = mysql_query($leadersql);
    
    while ($row = mysql_fetch_array($result)) {
      echo "hello";
    };


    where 'mytable' is a custom made table within the main ModX database.

    Currently I get nothing with the code above - however, this worked fine in Evo.

    Can anyone help me out?

    Thanks!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Revo uses xPDO, and extension of PDO. A PDO connection is not the same a raw mysql connection (they use different drivers); you cannot mix PDO requests and raw mysql requests in the same connection.

      http://rtfm.modx.com/display/xPDO20/Retrieving+Objects

        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
        • 3749
        • 24,544 Posts
        You might look at Shaun's RowBoat extra, which (IIRC) will pull data from non-xPDO tables for you.


        ------------------------------------------------------------------------------------------
        PLEASE, PLEASE specify the version of MODX you are using.
        MODX info for everyone: http://bobsguides.com/modx.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: sottwell at Sep 17, 2012, 11:52 PM
          Revo uses xPDO, and extension of PDO. A PDO connection is not the same a raw mysql connection (they use different drivers); you cannot mix PDO requests and raw mysql requests in the same connection.

          http://rtfm.modx.com/display/xPDO20/Retrieving+Objects


          I'm very confused...I've never even heard of PDO. It looks like an OOP language. How would I go about modelling a query such as above?
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            In this case, you should follow Bob's advice and install Rowboat. http://rtfm.modx.com/display/ADDON/Rowboat.Rowboat

            Rowboat uses chunk/tpls like Wayfinder or getResources so that you can format each row of the output as you wish.
              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
              • 40122
              • 330 Posts
              Or would it be easier if I just created a whole new database for my custom tables? Would normal PHP/MySQL work from snippets then?
                • 3749
                • 24,544 Posts
                I think that would make things worse, not better. wink Try RowBoat.

                The best option, in terms of speed, would be to convert your table(s) to xPDO and use Revolution's built-in xPDO methods to access them: http://bobsguides.com/custom-db-tables.html. You'd have to learn xPDO, but it's very easy to work with once you get used to it (and there are a lot of other things you can do with it in MODX Revolution).


                ------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using.
                MODX info for everyone: http://bobsguides.com/modx.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 Sep 18, 2012, 12:31 AM
                  I think that would make things worse, not better. wink Try RowBoat.

                  The best option, in terms of speed, would be to convert your table(s) to xPDO and use Revolution's built-in xPDO methods to access them: http://bobsguides.com/custom-db-tables.html. You'd have to learn xPDO, but it's very easy to work with once you get used to it (and there are a lot of other things you can do with it in MODX Revolution).


                  ------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using.
                  MODX info for everyone: http://bobsguides.com/modx.html

                  Thanks Bob - I think ill just have to learn it. I'm assuming RowBoat works great for out putting results from a query but I will need to save those results to variables and do some maths with them. I am assuming you cannot do this with RowBoat?
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    You can create your own connection to the database using mysql_connect.

                    Rowboat is nice, but of course being extremely flexible and generic can't quite match the performance of a custom-crafted query for specific data.

                    However, you'd be missing out on the power and ease-of-use (in the long run) of xPDO. It's well worth taking a few minutes and going through something like Bob's tutorial http://bobsguides.com/custom-db-tables.html

                      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
                      • 40122
                      • 330 Posts
                      Quote from: sottwell at Sep 18, 2012, 12:57 AM
                      You can create your own connection to the database using mysql_connect.

                      Rowboat is nice, but of course being extremely flexible and generic can't quite match the performance of a custom-crafted query for specific data.

                      However, you'd be missing out on the power and ease-of-use (in the long run) of xPDO. It's well worth taking a few minutes and going through something like Bob's tutorial http://bobsguides.com/custom-db-tables.html

                      Still having loads of difficulty, even with Bobs guide. Are you saying if I just used a traditional mysql_connect statement to connect to the ModX database followed by normal PHP/SQL in my snippet it would be ok?