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 3 of 3

Thread: PHP OOP Help

  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts

    PHP OOP Help

    Hello,
    Im learning OOP and I am trying to make a Database Connection Class. The following code is not making a successful connection:

    connectClass.php:
    PHP Code:
    <?php

    class MySqlConnect {
        
        
    //Variable Declatiation
        
    private $username;
        private 
    $password;
        private 
    $host;
        private 
    $dbname;
        
        
    //constructure
        
    public function __construct($user$pass$host$database){
            
    $this->username$user;
            
    $this->password $pass;
               
    $this->host $host;
               
    $this->dbname $database;
               
               
    //Make the Connection
               
    $connection = @mysql_connect ($host$user$pass) OR die ('Could not connect to MySQL: ' mysql_error() );
            if(
    $connection) {
                
    $dpmysql_select_db($database);
                if(!
    $db) echo "Failed to select $database";
            } 
                else {
                    echo 
    "Failed to connect to the database";
                    }
        }
    }

    ?>
    index.php:
    PHP Code:
    <?php

        
    include('connectClass.php');

        
    $db = new MySqlConnect('root''''localhost''cms');
        
    ?>
    When I run the script I get the error "cannot connect to cms".

    Any help would be great!

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You mixed up the variables:
    PHP Code:
    $dpmysql_select_db($database); // $dp
    if(!$db) echo "Failed to select $database";  // $db 

  • #3
    Regular Coder
    Join Date
    Jul 2007
    Posts
    571
    Thanks
    25
    Thanked 28 Times in 28 Posts
    Ugg, Thank!

    I'm allowed making dumb mistake like that because it is the holidays lol :-P


  •  

    Posting Permissions

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