views:

403

answers:

5

Hello,

My question is:

Can I still do query's from within an object like this:

$result = mysql_query ($q,$dbc) 
          or 
          trigger_error("Query: $q\n<br />MySQL Fout: " . mysql_error($dbc));

by passing the global dbconnection variable $dbc to the constructor

or is there a better way?

Or creating a singleton class for the databaseconnection, but I see a lot off negativity that people are writing about it.

I am new to making objects, so I don't know if I mayby have to do it all a little different, with the db I mean.

thanks, Richard

A: 

Yes, you can pass the database connection around like that.

IMHO it makes coding a little harder but testing a lot easier.

Greg
A: 

Here's how you would use it:

class DB {
    private $dbc;

    public function __construct($dbConn) {
        $this->dbc = $dbConn;
    }

    public function runQuery() {
        mysql_query($query, $this->dbc);
    }
}
RaYell
so, this class can I register with a registryclass like this, only it's not a singleton./*** create the database registry object ***/ // $registry->db = db::getInstance();**also, if I want to run a query, then the query should be an argument off the runquery method, right**Then the result should be exposed in a public variable then, is that how it's done?
Richard
include db_connect.php;///// this sets global $dbc;//////then////// $registry->db = new DB($dbc);***** then, i have created a global variable AND an object. Is that good, as far as doing stuff?**Also, apparently, I can not use trigger error anymore, should I @suppress any errors, coming from the query's?
Richard
This is the way I do it. I wouldn't suppress errors, at least in development. After the development is done, you can @suppress errors and provide more user friendly error messages.
EvilChookie
A: 

Pass the variable. Singletons have a bad reputation for a reason - they were invented to solve a very specific problem, and they didn't even do that particularly well. Don't use them as a way to avoid passing variables around, that's how it should be done.

Paul McMillan
A: 

I use a singleton class for this purpose, but I still pass around my class variable through globals.

The reason the singleton class was created in the first place was to make sure that only one instance of a class is ever created. In this case, I want to make sure that only one instance to the database is ever created. By placing the class as a singleton class, anyone who writes code that interfaces with this class will get the same connection. But, it is still not a replacement for globaling the variable.

For the Singleton class, here is an example:

 class datbasebase
   {
    static $class = false;
    static function get_connection()
    {
     if(self::$class == false)
     {
      self::$class = new database;
     }
     else
     {
      return self::$class;
     }
    }
    // This will ensure it cannot be created again.
    protected function __construct()
    {
                $this->connection = mysql_connect();
    }
        public function query($query)
        {
                return mysql_query($query, $this->connection;
        }
   }

   $object = database::get_connection();

The main reason I do it this way instead of simply passing around the connection is purely because I don't want to repeat code over and over. So it is a time saver to have my query, connect, and various other DB functions in the same class.

Chacha102
+1  A: 

If you looking for database abstraction, why not consider using the DB classes provided by Zend Framework.

They also include a way to set an adapter as the default, making it a snap to perform operations.

Add to that the fact that Zend_Db defaults to using parameterised queries instead of old fashioned quoted ones, thus adding security and peace of mind.

using globals is pretty much a bad idea, its far too easy to accidentally overwrite one! along with all the other reasons you will find for not using them with a quick google.

Making a db connection with Zend Framework is a snap,

$db = Zend_Db::factory('Pdo_Mysql', array(
'host'     => '127.0.0.1',
'username' => 'webuser',
'password' => 'xxxxxxxx',
'dbname'   => 'test'

));

It really is as simple as that. you can then either make your table classes to provide quick access to individual tables, or use the select and statement objects for more advanced joined querys.

Also, with Zend Framework, you do not have to use the whole stack, you can simply use just the DB components if you like.

If you don't want to use Zend Framework for this, I would strongly recommend you consider alternatives such as an ORM like Doctrine over writing your own DB abstraction. you will end up with a monster very quickly, and an ORM or Zend Framework have a great many developers squashing bugs, and even more reporting any that are there.

Bittarman