tags:

views:

44

answers:

2

Hello all. The following code connects my database

var $connection;

function MySQLDB(){
  $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
  mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());

etc etc

This code is within a class called MySQLDB

At the end

$database = new MySQLDB;

Inside that class I can connect to things using

$this->connection

How can I access this connection from outside the class within another class? Thanks

A: 

Add a function to the MySQLDB class that returns the connection

function GetConnection() {
    return $this->connection;
}

then later on:

$database = new MySQLDB;
$myconn = $database->GetConnection(); // your connection
pinkfloydx33
Will I be able to use this in other scripts?
Luke
If the scripts include the MySQLDB class, and assuming the connection's been opened, then why not?
pinkfloydx33
Yeah they do include this file.
Luke
Just having a few problems trying to understand how I want my classes to connect to each other, majorly confused! Thanks for your help!
Luke
By any chance are you using JPmaster77's scripts? THe naming looks really familiar. If so, I would suggest not returning the connection and adding functions to the MySQLDB class to perform all your DB operations and then call the methods that way: $database->DoSomethingWithData();
pinkfloydx33
Since the connection is public to begin with, you can just do $someObject->connection . No need to add an accessor. Now, if that is a good idea is another question.
geon
Pinkfloyd I started out using that script but have moved away from it a lot as I have improved. Looking to rewrite a lot of things but the database script is getting out of control. Got 1000s of lines of code in there now, surely that will become slow?
Luke
I guess if you optimize it a bit and make sure that you only deal with the actual database calls from that class and modify the session, form and process classes to perform the necessary function you should be fine. A few years ago I heavily modified his scripts for a project I was doing. I kept everything isolated within their respective classes and I never noticed any performance hits.
pinkfloydx33
Ok then thanks. I have been creating extra classes for other things but I just get confused as to where to put them. It is good to have someone that has used his scripts. Are they good? How can they be improved? If I call something in the database using $database->thisNow(); , is only that part of the database script used? Or does it load the entire script? That is my concern regarding the script getting too big. It also gets very messsy with 100s of queries within!
Luke
The whole file gets included by the include() statement. But when you call ->thisNow() obviously only that method is loaded. I'm not really too sure on the internals of PHP and how file size can affect things--but I never noticed anything. I used his login scripts to form the basis for a clothing store website I made as a project in college. I added all the necessary logic for anything a user or admin had to do. it was also a mixture of a facebook like site with comments, pictures and events (hard to explain) and I altered the process class to return xml for ajax calls instead of the default.
pinkfloydx33
Ahhh i see, sounds great. The way the scripts work interest me. I'm just wondering if its a standard thing to have a process class before a class that actually does the work!
Luke
A: 

If $connection not marked as private/protected, you can use $connection directly

$conn=$database->connection;

$res=mysql_query("SELECT blabla", $conn);

or

$res=mysql_query("SELECT blabla", $database->connection);

but better value is to use Singleton pattern http://en.wikipedia.org/wiki/Singleton_pattern

seriyPS
This is what I originally used!
Luke