No announcement yet.

Test for the Existence of a Database Within Try/Catch Block

  • Filter
  • Time
  • Show
Clear All
new posts

  • Test for the Existence of a Database Within Try/Catch Block

    I would like to test for the existence of a database (onApplicationInit) before attempting an sc_connection_edit (which will fail if the database does not exist).

    My approach was to use a standard PHP try/catch block, using the success or failure generated while creating a PDO object to accomplish this.

    The problem is I would like to use Scriptcase global database variables in the connection string, but [sc_glo_pass] has some internal encryption mechanism which causes the PDO connection to always fail. My solution was to use a clear text password in the application (not my preference), but I could not come up with an alternative.

    Here is the code:

    $dbpass = 'this_is_the_database_password_string'; // clear text password
    // Change db connection credentials
    $arr_conn = array();
    $arr_conn['database'] = [user_db_input];
    // Change db connection to point to proper db
    try {
    $connstr = "mysql:host=" . [sc_glo_server] . ";dbname=" . [user_db_input];
    $pdo = new PDO($connstr, [sc_glo_user], $dbpass);
    $pdo = NULL;
    sc_connection_edit("user_mysql", $arr_conn);
    catch (Exception $e) {

    Is there a way to accomplish the "try" portion of the block entirely in Scriptcase so that I can use [sc_glo_pass]?

    Alternately, is there a Scriptcase macro to unencrypt [sc_glo_pass] to pass it into the PDO connection string?