Location: PHPKode > projects > Php4dvd - movie database > php4dvd/install/files/create_db.php
<?php
/**
================================================================================
 LISENCE
================================================================================

    This file is part of php4dvd.

    php4dvd is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    php4dvd is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with php4dvd. If not, see <http://www.gnu.org/licenses/>.
**/

/**
    This page of the installer will modify the database, or create the database
    depending on your install type. (upgrade or new). At this point no matter what
    the install type is, the new config file should exist.
 */


//Get the settings
$loc="../../";
include_once('../config/config.php');

//Connect to the server
if (!mysql_connect($settings['db']['host'],$settings['db']['user'],$settings['db']['pass'])) {
    echo ('Unable to connect to database server. Please check your /config/config.php file. ' . mysql_error());
    exit;
}

//Create the db if it doesnt already exist, if so skip creating it.
function create_db () {
global $settings;
        //Check to see if the db already exists, if not create it
        if (getDb($settings['db']['name'])) {
                echo "No problem, but the database already exists. <br>";
        }else{
                //Define query to create the db
                $sql = 'CREATE DATABASE `'.$settings['db']['name'].'`';

                //Run query
                if (mysql_query($sql)) {
                        echo "Database created. <br>";
                }else{
                        //They should never get this.
                        echo "<span class='inputerror'>Unable to create database.</span>";
                        exit;
                }
        }
}

//This is a new install, so connect to the db and run the new script
function new_install () {
global $settings;
        //Connect to the database, else error out
        @mysql_select_db($settings['db']['name']) or die( "Unable to select database");

        //Depending on the install type run the script
        loadSQL("files/sql/php4dvd.sql");

        mysql_close();
}

//This is an upgrade, so connect to the db, check the current version, check the new version
//and loop through each upgrade script (if it exists) to get the db all up to date.
function upgrade_install () {
global $settings;
        //Connect to the database, else error out
        @mysql_select_db($settings['db']['name']) or die( "Unable to select database");

        //Get version
        $install_ver= getVersion('../config/version.default.inc.php');
        $current_ver= getVersion('../config/version.inc.php');

        //Loop through each install
        while ($current_ver < $install_ver) {
                //Get the next version
                $temp= $current_ver + .1;

                //Therefore the filename is
                $filename="files/sql/update-".$current_ver."-".$temp.".sql";

                //If the file exists run the upgrade between versions
                if (is_readable($filename)) {loadSQL($filename);}

                //Increment to the next version
                $current_ver = $temp;
        }



        mysql_close();
}

//This function takes a sql file created by phpmyadmin or similar products and splits each query
//in the file and executes them. For each one, if succesfful or failed the status gets outputed.
function loadSQL($file) {
        $handle = fopen($file, "r");
        if($handle) {
                $contents = fread($handle, filesize($file));
                $regels = split("\n", $contents);
                $query = "";
                foreach($regels as $regel) {
                        $comment = preg_match("/^-/", $regel);
                        $empty = strlen(trim($regel));
                        if(!$comment && $empty > 0) {
                                if(preg_match("/;$/", trim($regel))) {
                                        $query .= $regel;
                                        if (mysql_query($query)) {
                                          echo "Query done. <br>";
                                        }else{
                                          echo "<span class='inputerror'>Unable to perform query. ".mysql_error()."</span><br />";
                                        }
                                        $query = "";
                                } else {
                                        $query .= $regel;
                                }
                        }
                }
        }
        fclose($handle);
}
?>
Return current item: Php4dvd - movie database