Richard
Richard

Reputation: 4546

database query's from class object with php

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

Upvotes: 3

Views: 1552

Answers (5)

Bittarman
Bittarman

Reputation: 660

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.

Upvotes: 1

Tyler Carter
Tyler Carter

Reputation: 61557

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.

Upvotes: 0

Paul McMillan
Paul McMillan

Reputation: 20107

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.

Upvotes: 0

RaYell
RaYell

Reputation: 70414

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);
    }
}

Upvotes: 0

Greg
Greg

Reputation: 321628

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

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

Upvotes: 0

Related Questions