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

    I´ve got a module. Inside of this module is a form where you can configure the settings of a database. To check if connection works I use:#

    $db = new DBAPI($dbhost ,$dbname , $dbuse, $dbpass, $dbchar, $dbtblpre);
    $db->connect();
    $db->isConnected;

    The code works if the database exists, otherwise I get a « MODx Parse Error »
    Now the idea was to use:

    try 
    { 
    //do the connection 
    } 
    catch (Exception $e) 
    { 
    //return error
    }


    Using this, I get a

    ... Parse error: syntax error, unexpected '{' ...


    Any ideas?

    Greetings
      • 22303 MODX Staff
      • 10,725 Posts
      MODx is PHP 4 compatible and as such does not support the use of Exceptions in the core code. In other words, try catch will do nothing since no Exceptions are ever thrown, just standard PHP warnings and errors.
        • 7116
        • 6 Posts
        So, how can I check, if the entered connention is valid, without getting an SQL-Error?

        Greeting
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: derfragende at Dec 02, 2008, 10:04 AM

          So, how can I check, if the entered connention is valid, without getting an SQL-Error?

          Greeting
          What is the SQL error, the only error you described was:
          ... Parse error: syntax error, unexpected '{' ...

            • 7116
            • 6 Posts
            « Failed to create the database connection! »

            Which make sense smiley

            But I want to handle this mistake in a special case.

            Greetings
              • 22303 MODX Staff
              • 10,725 Posts
              Use @ error suppression I suppose. This simply isn’t possible in other ways currently.
                • 7116
                • 6 Posts
                That does not work as I expect it to work.

                This statement causes an error
                $db2->connect();


                Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'www.mytest.ds' (1) in ... dbapi.mysql.class.inc.php on line 87
                « MODx Parse Error »MODx encountered the following error while attempting to parse the requested resource:
                « Failed to create the database connection! »
                 
                Parser timing
                  MySQL: 	0.0006 s	(1 Requests)
                  PHP: 	1228268999.6146 s	 
                  Total: 	1228268999.6152 s	 


                When I use it like this
                @$db2->connect();


                I get
                « MODx Parse Error »MODx encountered the following error while attempting to parse the requested resource:
                « Failed to create the database connection! »
                 
                Parser timing
                  MySQL: 	0.0004 s	(1 Requests)
                  PHP: 	1228269370.4234 s	 
                  Total: 	1228269370.4237 s	 


                Any ideas?

                Greetings
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Ah, that’s because the DBAPI appears to automatically force the display of that error by calling a function called messageQuit():
                        if (!$this->conn = ($persist ? mysql_pconnect($host, $uid, $pwd) : mysql_connect($host, $uid, $pwd, true))) {
                           $modx->messageQuit("Failed to create the database connection!");
                           exit;
                        } else {
                           $dbase = str_replace('`', '', $dbase); // remove the `` chars
                           if (!@ mysql_select_db($dbase, $this->conn)) {
                              $modx->messageQuit("Failed to select the database '" . $dbase . "'!");
                              exit;
                           }
                  

                  You will need to modify that to return false so you can detect if the connection was not successful without getting that error display.

                  I use PDO (and xPDO) for connecting to external databases personally (and in the upcoming Revolution release), but it might be good to record this as a bug in Jira for the 0.9.6.x versions so it gets addressed.
                    • 25663 MODX Staff
                    • 12,272 Posts
                    Yes please record a bug. Thanks for poking around! smiley
                      Ryan Thrash, MODX Co-Founder
                      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                      • 7116
                      • 6 Posts
                      But I still need a workaroud. huh

                      Greetings