PHP Resources
Home
Books
Directories
Magazines
Non-English Sites
Online Communities
Tools
Tutorials and Articles
Web Hosting
PHP Functions
PHP News Groups *
PHP Reference
Smarty Reference
Pear Reference
PHP-GTK Reference

By submitting PHP Resources you own, or know of, you'll help us build the largest PHP Resource website on the net. Please double check that your resource doesn't already exist before you submit it!!. We thank you for helping make this a better website.









Resource Image Newest ResourcesPopular ResourcesTop Resources Resource Image
PHP Resources
Update Database

Update Database

Update Database -- Updating a database against a new schema

Update Database

Having MDB2_Schema to update your database, when its schema changes is also really easy. You can use the getDefinitionFromDatabase() method to determine the current database schema, and then just use updateDatabase() to do the actual update. However, you have to make sure, that getDefinitionFromDatabase() returns what you expect before you rely on it. See the respective chapter for more info.

<?php
require_once 'MDB2/Schema.php';

$options = array(
    'log_line_break' => '<br>',
    'idxname_format' => '%s',
    'debug' => true,
    'quote_identifier' => true,
    'force_defaults' => false,
    'portability' => false
);
$dsn = 'mysql://root:@localhost/MDB2Example';

$schema =& MDB2_Schema::factory($dsn, $options);

if (PEAR::isError($schema)) {
    $error = $schema->getMessage();
} else {
    // first run with queries disabled to make sure everything is allright
    $disable_query = true;

    $previous_schema = $schema->getDefinitionFromDatabase();
    $op = $schema->updateDatabase('schema.xml', $previous_schema, array(), $disable_query);

    if (PEAR::isError($op)) {
        $error = $op->getMessage();
    }
}

if (isset($error)) {
    var_dump($error);
}

$schema->disconnect();
?>

The method accepts both, a filename or a definition array, as the first two parameters. Note how we mixed them in the example above. You may want to backup the current schema using dumpDatabase() for the case something goes wrong.

When updating database schemas we can run into data persistence issues. This can be addressed with data manipulation ability, that will be documented later in this manual.





Featured




Featured
PHP Code Examples
web site templates
Learn PHP playing Trivia
PHP & MySQL Forums
Web Development Index

List Your ResourceUpdate Your Resource

Copyright © 2006 - 2008 MickMel Inc