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 4 of 4
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    Mysql Connection through Classes

    Okay, the problem is that my script is not connecting to the database.

    Here is the relavant code:

    index.php
    PHP Code:
    //This file contains the stored database information...username, password, database, and host in the INFO array
    require_once(ROOT_PATH."config.php");

    //Call up the super-global class
    include(ROOT_PATH."includes/superclass.php");

    //Start up the super-class, set up database connection, initialize variables
    $superclass = new superclass;
    $superclass->vars $INFO
    config.php
    PHP Code:
    // Initialize the INFO array
    $INFO = array();
    //Set up INFO variables
    $INFO['prefix'] = ""//Table Prefix
    $INFO['database'] = "blanked out for security"//Database to connect to
    $INFO['password'] = "blanked out for security"//database password
    $INFO['username'] = "blanked out for security"//database user
    $INFO['host'] = "blanked out for security"//database host
    $INFO['url'] = "blanked out for security"//Board URL 

    superclass.php
    PHP Code:
    class superclass
    {


        public 
    $vars = array();     //Superclass variable holder
        
    public $DB;                    //Superclass database holder
        
    public $err;                //Superclass error holder
        
    public $lang;                //Superclass Language holder

        //constructor
        
    function __construct()
        {
            
    //Initialize database connection
            
    require_once(ROOT_PATH."includes/functions/dconnect.php");
            
            
    //Add the DB class to the super
            
    $this->DB = new dconnect();
            
    //
            
    $this->DB->data['host']     = $this->vars['host'];
            
    $this->DB->data['username'] = $this->vars['username'];
            
    $this->DB->data['password'] = $this->vars['password'];
            
    $this->DB->data['database'] = $this->vars['database'];
            
    //Connect to the database
            
    $this->DB->connect();
    }

    dconnect.php
    PHP Code:
    class dconnect
    {
        public 
    $data = array(); //Contains the class data.
        
        //Tasty Connection-ness
        //Connects to the database and selects based on the stored data from the config file.
        
    function connect()
        {
            
    $this->data['connection'] = mysql_connect(        $this->data['host'], 
                                                            
    $this->data['username'], 
                                                            
    $this->data['password']
                                                        ) 
                                                        or die(
    'Could not connect to the database');
            
    $this->data['db'] = mysql_select_db($this->data['database'],$this->data['connection']) or die('Could not find database');
        }

    and the error being returned is:

    Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /mounted-storage/home45c/sub001/sc21473-GRUR/dev/forum/includes/functions/dconnect.php on line 35
    Could not connect to the database
    I know that all the variables in the config are set properly. ROOT_PATH is definitely correct, and all the files are being called from the right location.

    Does anyone have any idea what the problem is? Ive been looking at this for quite some time now and I cant figure it out. I could really use some help.

  • #2
    New Coder
    Join Date
    Jul 2005
    Posts
    81
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Check out mysql server. If you have installed phpmyadmin open it and see the status of server. The problem is not in your code.
    If you can not find a decision, maybe you have to try to change a problem

    http://www.gunman.co.nr
    http://bglinux.org

  • #3
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    Im pretty sure if not positive that it is in my code, I can run a direct

    PHP Code:
    mysql_connect(        $INFO['host'], 
                                                            
    $INFO['username'], 
                                                            
    $INFO['password']
                                                        ) 
                                                        or die(
    'Could not connect to the database'); 
    from my config file and it works. So something is happening to it that is messing it up while it is being used in other files.

  • #4
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    I fixed it.

    It was because on the index page:

    $superclass = new superclass;
    $superclass->vars = $INFO;

    but the $info was needed before the superclass was run because it was used in a construct. So I made the construct a function and called the function after the $vars were defined.

    Thanks guys, you helped me figure it out.


  •  

    Posting Permissions

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