NYCPHP Meetup

NYPHP.org

[nycphp-talk] Database creation (YAML VS hard coded PHP)

Ben Sgro (ProjectSkyLine) ben at projectskyline.com
Sun Oct 28 11:25:23 EDT 2007


Hello all,


#1 Thanks for the ideas on CSS (my last question). I still haven't been able to invest the type of time
I would like to play around with the code, but I will. I got the CSS Mastery book and I've got an APress
book on HTML/CSS design patterns. Both look promising.

I started off doing db deployment code in a seperate file, say createdb.php that I would run from
the URL.

include ('PROS.php');
    include ('../LIBRARY/DBAS.php');

    DBAS_InitDBHandle(PROS_SQLInfo( ), 'PROS.php', $db);
    DBAS_MySQLUseDB(constPROSDBStr, $db);
    
    $tableSet = DBAS_FetchTables(constPROSDBStr, $db);
    if ( @ !$tableSet[constContentStr]) 
    {
        error_log("createdb.php: creating 'content' table");
        $sqlStr = "CREATE TABLE content "
                . "(id int(11) NOT NULL auto_increment,"
                . " display_on    int(11) default 0,"
                . " title         varchar(64)   default '',"
                . " body          text(8192) default '',"
                . " thumb_img     varchar(64)   default '',"
                . " page_img_1    varchar(64)   default '',"
                . " page_img_2    varchar(64)   default '',"
                . " img_1_caption  varchar(64)   default '',"
                . " img_2_caption  varchar(64)   default '',"
                . " img_1_alt      varchar(64)   default '',"
                . " img_2_alt      varchar(64)   default '',"
                . " page_position varchar(32)   default '',"
                . " link          varchar(64)   default '',"
                . " primary key(id)"
                . ')';
        DBAS_MySQLQuery($sqlStr, $db);
    }

After a while, that was kinda of a pain and I moved to something internal to the index.php?action=create_database file
and cleaned up my database object code.

        /* Comments table. */
        $dbObject->DatabaseCreateTable(DATABASE_TABLE_COMMENTS);
        $dbObject->DatabaseCreateField('comment_id', 'int(11)', 'NOT NULL',
                                       'auto_increment');
        $dbObject->DatabaseCreateField('video_id',    'int(11)',     DEF_0);
        $dbObject->DatabaseCreateField('posted_time', 'varchar(32)', DEF_0);
        $dbObject->DatabaseCreateField('posted_by',   $v64,          $defE);
        $dbObject->DatabaseCreateField('comment',     'varchar(512)',$defE);
        $dbObject->DatabaseCreateField('primary', 'key(comment_id)');
        $dbObject->DatabaseCreateCommit(LOG_LEVEL_DEBUG);   

Which I used for a while and I liked it. Except its a lot to type for larger databases.
So I started messing with YAML and I've got my code down to this:

emails:
    id:
        type:    int(11)
        default: not null
        extra:   auto_increment
    email:
        type:    varchar(255)
        default: default 0
        extra:
    contacted:
        type:    tinyint(1)
        default: default 0
        extra:
    primary:
        type:    key(id, email)
        default: ""
        extra: 

Which I just feed into a method that creates the table. I have this code only execute 
when I pass a command line switch in, say php index.php -f db.yaml. If I don't have
CL access then I'd have to move it to the URL.

Do you all think agree this is a better direction ? What are some pitfalls I could run into 
doing db's this way?

Thanks so much!

- Ben

Ben Sgro, President
ProjectSkyLine - Defining New Horizons
+1 718.487.9368 (N.Y. Office)

Our company: www.projectskyline.com
Our products: www.project-contact.com

This e-mail is confidential information intended only for the use of the individual to whom it is addressed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20071028/8e56a192/attachment.html>


More information about the talk mailing list