Welcome to WebmasterWorld Guest from 54.166.46.226

Forum Moderators: open

Message Too Old, No Replies

Mysql link identifier

   
7:23 pm on Mar 3, 2010 (gmt 0)

5+ Year Member



Hello,

I am using simple mysql adapter class to connect on mysql server:

class MySQL {

private $connection;

function __construct() {
$this->dbConnect();
}

private function dbConnect() {
$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());
}

private function dbClose() {
mysql_close($this->connection);
}

public function query($query) {
return mysql_query($query, $this->connection);
}
};
$mysql = new MySQL;

class Get{

private $db;

function __construct() {
global $mysql;
$this->db = $mysql;
}

function get() {
$q = "SELECT ...";
$result = $this->db->query($q);
}

};
$get = new Get;


Is this the right approach?

Firstly I've tried to extend MySQL class as follows:

class Get extends MySQL {

function get() {
$q = "SELECT ...";
$result = $this->query($q);
}

};


but I'm constantly getting error "...not a valid MySQL-Link resource". If I remove $this->connection as second argument in mysql_query(), than "extends" approach works... Why is this happens?

I'd appreciate some thoughts on this topic cose I'm trying to get some standard class to connecto to mysql.

Thanks
9:47 pm on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



In one instance of your get() function, you reference a ->db and in the second you don't. Perhaps the error is not including the ->db in the second instance (in the one that extends the first)?