We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8779
    • 93 Posts
    I want to open new connection DB in my snippet, but it can’t
    The modx table won’t work because the connection is wrong.
    How to solve this problem?

    thank you
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Open the connection when you need to access the external database, make your query and save the results, then close the connection. The script should now revert to the original (MODx) connection to the MODx database, and you will have the data you retrieved from the external database.
        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
        • 8779
        • 93 Posts
        Fool me. I was run it before I put mysql_close() in the snippet.
        Thanks sotwell laugh
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Hey, us elven Rangers have to stick together!
            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
            • 8779
            • 93 Posts
            lol
            Hey it didn’t work again.
            Sorry it’s my bad.

            this is the error message

            « MODx Parse Error »
            MODx encountered the following error while attempting to parse the requested resource:
            « Execution of a query to the database failed - Table ’resepkita.resep_atrating_grpname’ doesn’t exist »
            SQL: SELECT rating_id as ’params’, rating as ’nbStars’, lastIP as ’atrTpl’, nbIP, voteDate as ’endDate’ FROM resep_atRating_grpName WHERE id = ’1’
            [Copy SQL to ClipBoard]
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Can you post your code?
                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
                • 8779
                • 93 Posts
                This is my code

                include_once $modx->config['base_path'].'/ResepKita/common/lib/php/configuration.php';
                mysql_connect($MY_CONF["mysql_host"],$MY_CONF["mysql_user_name"],$MY_CONF["mysql_user_password"]);
                mysql_select_db($MY_CONF["mysql_database_name"]);
                mysql_close();


                actually I use anythingRating ad-ons for my web.
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  um... why are you closing the connection before you use it?

                  You should get the connection variables and use them
                  include_once $modx->config['base_path'].'/ResepKita/common/lib/php/configuration.php';
                  $newconn = mysql_connect($MY_CONF["mysql_host"],$MY_CONF["mysql_user_name"],$MY_CONF["mysql_user_password"]);
                  mysql_select_db($MY_CONF["mysql_database_name"], $newconn);
                  // do your database queries here
                  $myresults = mysql_query('blah...', $newconn);
                  mysql_close($newconn);

                  do whatever to $myresults now.

                  http://us.php.net/manual/en/ref.mysql.php
                    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
                    • 8779
                    • 93 Posts
                    I just test the connection, so I will use it when the connection is working.
                    I’ll try that.

                    EDITED
                    Yes it is works, thanks a lot sotwell.
                    Sorry for the long time to response this post, I was sick yesterday.