Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts

    OOP database class problem

    Ok, so I built a database class using OOP. I can't seem to figure out what is wrong with my code. Any suggestions?? Hint, I've just started learning how to code using Object Oriented php. Obviously, I have a config file required once in the script of which I will not put on here, but most of you know what you can do with that.

    PHP Code:


    <?php
    require_once("config.php");

    class 
    MySQLDatabase {
        
        private 
    $connection
        
        function 
    __construct() {
            
    $this->open_connection();
        }
        
        public function 
    open_connection() {
            
    $this->connection mysql_connect(DB_SERVERDB_USERDB_PASS);
            if (!
    $this->connection) {
                die(
    "Database connection failed: " mysql_error());
            } else {
                
    $db_select mysql_select_db(DB_NAME$this->connection);
                if (!
    $db_select) {
                    die(
    "Database selection failed: " mysql_error());
                }
            }
        }

     public function 
    close_connection() {
         if(isset(
    $this->connection)) {
             
    mysql_close($this->connection);
             unset(
    $this->connection);
         }
     }
     
     public function 
    query($sql) {
         
    $result mysql_query($sql$this->connection);
         
    $this->confirm_query($result);
         return 
    $result;
     }
      public function 
    mysql_prep$value ) {
        
    $magic_quotes_active get_magic_quotes_gpc();
        
    $new_enough_php function_exists"mysql_real_escape_string" ); // i.e. PHP >= v.4.3.0
        
    if( $new_enough_php ) { //PHP v4.3.0 or higher
        //undo any magic quote effects so mysql_real_escape_string can do the work 
            
    if( $magic_quotes_active ) { $value stripslashes$value ); }
            
    $value mysql_real_escape_string$value );
        } else { 
    // before PHP v4.3.0
        // if magic quotes aren't already on then add slashes manually 
        
    if( !$magic_quotes_active ) { $value addslashes$value ); }
        
    // if magic quotes are active, then the slashes already exist
        
    }
        return 
    $value;
    }

      private function 
    confirm_query($result) {
         if (!
    $result) {
             die(
    "Database query failed: " mysql_error());
         }
     }
     
     

    }

    $database = new MySQLDatabase();
    $db =& $database;


    ?>
    Last edited by Chris Hick; 05-03-2011 at 07:19 PM.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by Chris Hick View Post
    ....I can't seem to figure out what is wrong with my code....................... Any suggestions??
    Hint: posting the above without any more details about what errors or incorrect functionality you are getting is a bit like going to your doctor and saying 'Hi Doc, I'm not feeling well. Please fix me" without telling him/her what your symptoms are.

  • #3
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    I'll take a guess and suggest that maybe you just need to do some Basic Debugging 101

  • #4
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    Eh, yeah sorry man my brain is fried at the moment. Been working a 16 hours shift to come home and trying to fix up my code to no avail. My apologies lol. But when I make an index.php that looks like this:
    PHP Code:
    <?php
    require_once("../includes/database.php");

    if(isset(
    $database)) { echo "true";} else { echo "false"; }
    echo 
    "<br />";

    echo 
    $database->mysql_prep("It's working?<br />");

    ?>
    It just stays in the loading(connecting) state on my browser. I am using this from within local host on my computer and use WAMP. Then, after a while, it says error, code runs out on line 13.
    Correction, this is what it says:
    Warning: mysql_connect() [function.mysql-connect]: MySQL server has gone away in C:\wamp\www\wejustcan1\includes\database.php on line 13

    Last edited by Chris Hick; 05-03-2011 at 02:58 AM.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #5
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Id put
    PHP Code:
    $database = new MySQLDatabase(); 
    on the index page rather than the database.php page.

  • #6
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    Tom, that would defeat the purpose of OOP.

    Anyhow, I figured out the problem. My code was flawless, but my php setup inside my local host was not right. I tried it out on my site's server and it worked just as it should.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •