Jump to content

MySQL Wrapper


gary00ie

Recommended Posts

Hi,

 

I am trying to find the best (best as in fastest and OOP) MySQL connection wrapper to connect, query, insert, delete and query a MySQL DB.

 

I want to do this so I don't have to keep flipping from one or two that I have which are sluggish and also doesn't do everything I need.

 

I am coming from a JAVA background with a little experience and I learn well from seeing code.

 

I would just love to see some code that will do the above in a clean and efficient manner.

 

I also want to do this very OOP style. 

My questions are:

- do I need abstract classes or interfaces or is that overkill?

 

Also, is it a good idea to keep the db user / pass etc. in the database wrapper class?  Or, would they be better off in a config file which is included?

 

I have been away from programming for almost two years and would very much appreciate some guidance and I am not so much asking for a handout in regards to code, but, to get me started again, could someone please give me a little guidance here, as in what I need to do with perhaps a bit of code?

 

I want to make this class fully OOP with try / catch blocks etc.

 

Thanks in advance.  I really appreciate any help you guys can give.

Link to comment
Share on other sites

This is what I'm using can't recall the reference I used but as far as speed, I never tested it as it just worked fine for what I needed.

 

<?php
/* Class for working with the databases */

class Database
    {
    var $Host     = "localhost"; // Hostname of our MySQL server.
    var $Database = ""; // Logical database name on that server.
    var $User     = ""; // User and Password for login.
    var $Password = "";
    
    var $Link_ID  = 0;  // Result of mysql_connect().
    var $Query_ID = 0;  // Result of most recent mysql_query().
    var $Record   = array();  // current mysql_fetch_array()-result.
    var $Row;           // current row number.
    var $LoginError = "";

    var $Errno    = 0;  // error state of query...
    var $Error    = "";
    
//-------------------------------------------
//    Connects to the database
//-------------------------------------------
    function connect()
        {
        if( 0 == $this->Link_ID )
            $this->Link_ID=mysql_connect( $this->Host, $this->User, $this->Password );
        if( !$this->Link_ID )
            $this->halt( "Link-ID == false, connect failed" );
        if( !mysql_query( sprintf( "use %s", $this->Database ), $this->Link_ID ) )
            $this->halt( "cannot use database ".$this->Database );
        } // end function connect

//-------------------------------------------
//    Queries the database
//-------------------------------------------
    function query( $Query_String )
        {
        $this->connect();
        $this->Query_ID = mysql_query( $Query_String,$this->Link_ID );
        $this->Row = 0;
        $this->Errno = mysql_errno();
        $this->Error = mysql_error();
        if( !$this->Query_ID )
            $this->halt( "Invalid SQL: ".$Query_String );
        return $this->Query_ID;
        } // end function query

//-------------------------------------------
//    If error, halts the program
//-------------------------------------------
    function halt( $msg )
        {
        printf( "</td></tr></table><b>Database error:</b> %s<br>n", $msg );
        printf( "<b>MySQL Error</b>: %s (%s)<br>n", $this->Errno, $this->Error );
        die( "Session halted." );
        } // end function halt

//-------------------------------------------
//    Retrieves the next record in a recordset
//-------------------------------------------
    function nextRecord()
        {
        @ $this->Record = mysql_fetch_array( $this->Query_ID );
        $this->Row += 1;
        $this->Errno = mysql_errno();
        $this->Error = mysql_error();
        $stat = is_array( $this->Record );
        if( !$stat )
            {
            @ mysql_free_result( $this->Query_ID );
            $this->Query_ID = 0;
            }
        return $stat;
        } // end function nextRecord

//-------------------------------------------
//    Retrieves a single record
//-------------------------------------------
    function singleRecord()
        {
        $this->Record = mysql_fetch_array( $this->Query_ID );
        $stat = is_array( $this->Record );
        return $stat;
        } // end function singleRecord

//-------------------------------------------
//    Returns the number of rows  in a recordset
//-------------------------------------------
    function numRows()
        {
        return mysql_num_rows( $this->Query_ID );
        } // end function numRows
        
    } // end class Database 
?>

Link to comment
Share on other sites

Hi there,

 

Thanks so much for the reply, I appreciate it.

 

This is not really wht I am looking for though.  That is kind of what I already have.

I want total OOP as in no var to define variables.  I am looking more for private, protected etc. for my variable declarations.

 

Again, thanks so much for your time.

Link to comment
Share on other sites

Oh no, I don't want to over complicate it at all, in fact, just the opposite.  I want the simplest, efficient, yet elegant class. 

However, what I also want is to have strictly OOP PHP5 code, no var's to define variables, just private, public etc. and a solid class with try catch's to handle errors.

 

I want to learn from it and hopefully modify it to use as my default db wrapper for future projects.

 

Sorry if I made that unclear in my original post.

Link to comment
Share on other sites

I'm tagging this to see where it leads because I'm interested myself now as to what advantages this has over using a traditional class, it certainly is clear now as to what you want lol!

 

Great, I eagerly await a complete PHP OOP solution to this.

 

I'm really interested to hear further responses as it's really interesting to me and I just think a good example would be beneficial for future development from many developers.

 

I guess, to further clarify, I just would like to have a MySQL wrapper class that is fully OOP (with or without an abstract class or interface) and with solid functions which is secure and I can't seem to find what I need.  This is both for a learning experience and also to have a base for something I can modify, expand upon and share.

 

I hope to get some more responses and thanks for all the replies thus far.

Link to comment
Share on other sites

I believe that you are looking for any active record implementation out there.  Why not google for one instead of asking us to provide you with one?  There are a ton out there if you just take a bit of time and search for it ...

 

~awjudd

Link to comment
Share on other sites

I believe that you are looking for any active record implementation out there.  Why not google for one instead of asking us to provide you with one?  There are a ton out there if you just take a bit of time and search for it ...

 

~awjudd

 

Thank you for your response.

 

I did google it, which is something I always do before posting on a forum.  I was just trying to find a solution better than the one's that I found that were not very good.  That s why I posted here on a PHP forum.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.