37 users online (0 members and 37 guests)  


  Results 1 to 4 of 4

Related

  1. Replies: 1
  2. div layer question    Forum: Myspace Forum
    Replies: 1
  3. the PERFECT scrolling layer???!?    Forum: CSS Forum
    Replies: 1
  4. Scrolling in layer?    Forum: CSS Forum
    Replies: 2
  1. #1
    Zaire's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Mar 2001
    Location
    Hamburg, NY
    Posts
    312

    MySQL DB Abstraction Layer

    Well, in writing sitemod 1.0 http://sitemod.net I needed to write some db abs. layers..

    I wrote this mysql one, and would like some public testing of it...

    Could anyone test it out, and see if you find anything wrong

    PHP Code:
    <?php
    /*
    Script Name: mysql_layer.php
    Script Author: ZaireWeb Solutions [zaireweb.com]
    Project Title: sitemodCMS
    Project Home: sitemod.net
    ----------------------------------------------------------------
    ||                         Change Log                         ||
    ----------------------------------------------------------------

    -- Version: 0.4.0
       - Initial Release

    -- Version: 0.6.2
       - Converted to short hand expressions
         - Fixed $_layer_name to it's correct value
         - added con();
                 function conn($db = "FALSE")
                 - checks for a current database connectio
                 - if it's available, it doesn't do anything
                 - if there is -NO- connection, is starts one
                 
         - added close_all()
                 function close_all()
                 - Does NOTHING for mysql... (odbc...)
                 - just to keep in form with my othe DB Abs. Layers

    -- Version 0.6.3
       - removed conn() and made it a pointer to open()
          
    -- Version 0.6.3
       - Totaly removed conn()

    ----------------------------------------------------------------
    ||                         TO-DO                              ||
    ----------------------------------------------------------------
    -- Integrate with the CP Engine
       - Automaticlly run $cp->prep_fromdb and $cp->prep_intodb

    -- Error Handle'n
       - Output to some type of error log
       - expand the error handle'n capabilities

    -- Add Persistant Connection Availablitiy
    ----------------------------------------------------------------
    */
    class sql {
        var 
    $_app_version '0.6.3-dev';
        var 
    $_layer_name 'mysql';
        function 
    layer($db "FALSE")
        {
            
    $this->open($db);
        }
        function 
    version()
        {
            return 
    $this->_app_version;
        }
        var 
    $_id;                                // hold connection id
        
    var $_result_id;
        var 
    $_num_rows;
        var 
    $_field_names = array();

        var 
    $db_host;                           // database host
        
    var $db_user;                           // database user
        
    var $db_pass;                           // datbase pass
        
    var $db_main;                           // main database
        
    var $db;                                // the current database

        
    var $sql;                               // hold the last query

        
    var $q_count 0;                       // how many query's have been run
        
    var $limit;
        var 
    $start;
        function 
    q_count()
        {
            return(
    $this->q_count);
        }

        function 
    sql()
        {
            
    $this->db_host   DBHOST;
            
    $this->db_user   DBUSER;
            
    $this->db_pass   DBPASS;
            
    $this->db_main   CPENGINE;
        }

        function 
    open($db "FALSE")
        {
            if( (!isset(
    $this->_id)) || ($this->_id == "") )
            {
                
    $this->db = ($db == "FALSE") ? $this->db_main $db;
                
    $this->_id mysql_connect($this->db_host$this->db_user$this->db_pass) or $this->error("Unable to connect to database Server");
                @
    mysql_select_db($this->db);
            }

            return 
    $this->_id;
        }
        
        function 
    query($query_text)
        {
            
    $this->q_count = (isset($this->q_count)) ? $this->q_count 1;
            
    $this->sql $query_text;
            return((
    $this->_result_id = @mysql_query($query_text$this->_id)) ? $this->_result_id FALSE);
        }
        
        function 
    fetch_array($result_id "")
        {
            global 
    $cp;
            
    $resuld_id = ($result_id == "") ? $this->_result_id $result_id;
            return ((
    $array = @mysql_fetch_array($result_id)) ? $array FALSE);
        }

        function 
    fetch_row($result_id "")
        {
            
    $resuld_id = ($result_id == "") ? $this->_result_id $result_id;
            return ((
    $array = @mysql_fetch_row($result_id)) ? $array FALSE);
        }

        function 
    num_rows($result_id "")
        {
            
    $resuld_id = ($result_id == "") ? $this->_result_id $result_id;
            return ((
    $this->_num_rows = @mysql_num_rows($result_id)) ? $this->_num_rows FALSE);
        }
        
        function 
    insert_id()
        {
            return((
    $return = @mysql_insert_id()) ? $return FALSE);
        }
        
        function 
    close()
        {
            @
    mysql_close($this->_id);
            @
    mysql_free_result();
        }
        
        function 
    close_all()
        {
            
    // no value in mysql
        
    }

        function 
    free_result($result)
        {
            return((@
    mysql_free_result($result)) ? TRUE FALSE);
        }

        function 
    error($msg)
        {
            
    $new_msg $msg .
                              
    "\n\n\n<!-- MySQL Said: " .
                              
    mysql_error() .
                              
    " -->\n\n<br><br>";
            return 
    $new_msg;
        }
    }
    ?>
    If you don't understand how to use it, post and I will explain.

  2. #2
    HTML's Avatar
    Administrator

    Status
    Offline
    Join Date
    Aug 2000
    Posts
    3,445

    Follow HTML On Twitter Add HTML on Facebook Add HTML on Google+ Add HTML on Linkedin Visit HTML's Youtube Channel
    Talk to me Zaire

    I uploaded and tried to access via browser and get a blank page.

    Dave
    AHFBWEB Less customers per server, more power for you!

    Business Class Shared Hosting

  3. #3
    Derek's Avatar
    Be Yourself

    Status
    Offline
    Join Date
    Aug 2001
    Location
    St. George, Utah, USA
    Posts
    466
    ya same here when I tested it a while ago on localhost I just forgot to reply...

    so do you like usin object-oriented programming? I don't much like it...

  4. #4
    Zaire's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Mar 2001
    Location
    Hamburg, NY
    Posts
    312
    If you upload and access via a browser, it will be blank

    You would need to actually do something with it, i.e change this portion

    PHP Code:
    function sql()
    {
         
    $this->db_host   DBHOST;
         
    $this->db_user   DBUSER;
         
    $this->db_pass   DBPASS;
         
    $this->db_main   CPENGINE;

    to reflect your values, db_main should be the main db..

    Then you would start it like so:

    PHP Code:
    $dbi = new sql;
    $dbi->open();
    $result $dbi->query("SELECT * FROM table");
    while(
    $data $dbi->fetch_array($result))
    {
         echo(
    $data['some_field_name'] . '<br>');

    I already found and fixed one minor bug.. num_rows() wasn't returning anything if it had a value of 0...



Tags for this Thread