...

View Full Version : Mysql Connection through Classes



thesavior
03-16-2007, 03:16 AM
Okay, the problem is that my script is not connecting to the database.

Here is the relavant code:

index.php


//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


// 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


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


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.

gunman
03-16-2007, 02:36 PM
Check out mysql server. If you have installed phpmyadmin open it and see the status of server. The problem is not in your code.

thesavior
03-16-2007, 03:35 PM
Im pretty sure if not positive that it is in my code, I can run a direct



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.

thesavior
03-16-2007, 04:34 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum