common::pdoOpen PHP Method

pdoOpen() public method

Open a connection to the database.
public pdoOpen ( )
    function pdoOpen()
    {
        require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "settings.class.php";
        $settings = new settings();
        switch ($settings::db_driver) {
            case 'mysql':
                $dsn = "mysql:host=" . $settings::db_host . ";dbname=" . $settings::db_database;
                break;
            case 'sqlsrv':
                $dsn = "sqlsrv:server=" . $settings::db_host . ";database=" . $settings::db_database;
                break;
            case 'pgsql':
                $dsn = "pgsql:host=" . $settings::db_host . ";dbname=" . $settings::db_database;
                break;
            case 'sqlite':
                // In v2.5.0 the path to the SQLite database is no longer hard coded.
                // So if there is a problem getting the path the the SQLite database
                // from settings.class.php we must use the old style hard coded path.
                $dsn = "sqlite:" . $settings::db_host;
                if ($dsn == "sqlite:") {
                    // Use the legacy hard coded path for older systems being updated before v2.5.0.
                    $dsn = "sqlite:" . $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . "portal.sqlite";
                }
                break;
        }
        $dbh = new PDO($dsn, $settings::db_username, $settings::db_password);
        if ($settings::db_driver == 'sqlite') {
            $dbh = new PDO($dsn);
        }
        if ($settings::pdo_debug == TRUE) {
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        return $dbh;
    }

Usage Example

Exemplo n.º 1
0
function upgrade()
{
    require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "common.class.php";
    require_once $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . "classes" . DIRECTORY_SEPARATOR . "settings.class.php";
    $common = new common();
    $settings = new settings();
    try {
        // Add the positions.aircraft column if the portal is using MySQL or SQLite database.
        if ($settings::db_driver == "mysql") {
            // Check to see if the column already exists.
            $dbh = $common->pdoOpen();
            if (count($dbh->query("SHOW COLUMNS FROM `" . $settings::db_prefix . "positions` LIKE 'aircraft'")->fetchAll()) == 0) {
                // Add the column if it does not exist.
                $sql = "ALTER TABLE " . $settings::db_prefix . "positions ADD COLUMN aircraft BIGINT";
                $sth = $dbh->prepare($sql);
                $sth->execute();
                $sth = NULL;
            }
            $dbh = NULL;
        }
        if ($settings::db_driver == "sqlite") {
            // Check to see if the column already exists.
            $dbh = $common->pdoOpen();
            $columns = $dbh->query("pragma table_info(positions)")->fetchArray(SQLITE3_ASSOC);
            $columnExists = FALSE;
            foreach ($columns as $column) {
                if ($column['name'] == 'lastSeen') {
                    $columnExists = TRUE;
                }
            }
            // Add the column if it does not exist.
            if (!$columnExists) {
                $sql = "ALTER TABLE " . $settings::db_prefix . "positionss ADD COLUMN aircraft BIGINT";
                $sth = $dbh->prepare($sql);
                $sth->execute();
                $sth = NULL;
            }
            $dbh = NULL;
        }
        // Update the version and patch settings..
        $common->updateSetting("version", "2.1.0");
        $common->updateSetting("patch", "");
        // The upgrade process completed successfully.
        $results['success'] = TRUE;
        $results['message'] = "Upgrade to v2.1.0 successful.";
        return $results;
    } catch (Exception $e) {
        // Something went wrong during this upgrade process.
        $results['success'] = FALSE;
        $results['message'] = $e->getMessage();
        return $results;
    }
}
All Usage Examples Of common::pdoOpen