views:

203

answers:

3

I am creating a database connection object, and I'm not sure what the best way would be...

If I create a dbConnection class which establishes the connection (using mysql_connect) can I then call mysql_query from anywhere in the main script?

what about from inside OTHER classes? Do i need to pass the object or attribute as parameters?

Basically, I'm looking for the best way (in terms of speed + memory) of connecting to the database ONCE using a simple class...

I've posted my constructor for the Database Connection class:

function __construct($database="") {
if (!empty($database)){ $this->db = $database; }
$this->dblink = mysql_connect($this->host,$this->username,$this->password);
if (!this->dblink) {
 //Fatal Error
}
mysql_select_db($this->db, $this->dblink);

}

+1  A: 

Rather than create your own database abstraction class, I would recommend giving ezSQL a try. The author did an excellent job of making it very easy to use. The data structures returned from the queries done by the class are far more flexible than those returned by mysql_query and it would save you a lot of work.

Justin Bennett
+1  A: 

You're almost there. My database class goes something like this:

class DB {
  function __construct($host,$user,$pass,$db) {
    // error checking has been removed for brevity
    $this->mysql = mysql_connect($host,$user,$pass);
    mysql_select_db($this->mysql, $db);
  }
  function fetchRows($query) {
    // error checking removed again
    $result = mysql_query($this->mysql);
    $return = array();
    while($data = mysql_fetch_assoc($result))
      $return[] = $data;
    mysql_free_result($result);
    return $return;
  }
}

In my main include file, I then have something like this:

require 'DB.inc.php';
$GLOBALS['DB'] = new DB('localhost', 'root', '', 'my_database');

And then in any part of my project I can run a query like this:

global $DB;
$query = 'SELECT UserID, UserName FROM users';
foreach($DB->fetchRows($query) as $user)
    echo "$user[UserID]: $user[UserName]\n";

Notes: mysql_pconnect() is not recommended, see the manual page user comments for some reasons why.

too much php
A: 

When you want to execute a query, you should pass the connection object as a parameter to the query function. You need access to the connection object anywhere you want to run a query.

require_once DBConnection;
require_once SomeClass;

$con = new DBConnection();
$query = "Select * FROM Users";
mysql_query($query, $con->link);

// Use the connection in another class that takes a connection object in the constructor
$someclass = new SomeClass($con);
$someclass->executeQueryThree();

However, I'd be more inclined to have an mysql_query within your DBConnection class as a method like so:

public function query($query) { $result = mysql_query($query, $this->link); return $result; }

And then do the following:

$query = "Select * FROM Users";
$con = new DBConnection();
$result = $con->query($query);

// From within SomeClass
$someclass = new SomeClass();
$result = $con->query($someclass->getQuery3());
Josh Smeaton