php mysql connection question

Associate
Joined
4 Mar 2007
Posts
315
Location
United Kingdom
So i've built my basic php cms but I have to keep stating the sql connection to db each time. I looked up a bit on it and it seems very OOP related in the way I can carry out a config file which holds the strings for the connection details. How would I go about referencing the connection details and carrying out the same connect to database each time?

Would this work?

config.php
PHP:
class config {

var $host = "a";
var $username = "b";
var $password = "c";
var $db_name = "d";
var $table = "e";

 function config(){

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

 }

}

now suppose I've logged in etc and everything is fine and I want to display something from the database yet i need to reconnect to the db. would this work?

something.php
PHP:
class something extends config{

var $dbQuery = (sql query goes here);
var $hello = ("hello world" . $dbQuery);
function something(){
config();
echo($hello);
}
}

Would any of this work at all?
 
ahh brilliant, I'm trying to get my head around the OOP side of php it seems similar to Java but I can't say im good at that either xD
 
Back
Top Bottom