I’ve just switched over to using ADODB with MySQL and PHP… I’m having troubles with connecting to the database. Before, I could just have one global connection to the database and run queries of of that, but now it seems I need a new connection in each class?
Is that how it’s supposed to work or am I doing something wrong?
Connection made in config file:
include 'admin/db/adodb.inc.php';
$DB = NewADOConnection('mysql');
$DB->Connect($server, $db_user, $db_pwd, $db);
Class used:
class getuser {
public function __construct($user_id) {
$userquery = $DB->Execute("SELECT * FROM users WHERE id = '".$user_id."'");
while($user = $userquery->FetchRow()){
$this->uid = $user['id'];
$this->username = $user['username'];
$this->email = $user['email'];
}
}
}
The variables used to connect are all set fine. If I were to include the config file itself within then class the it works fine, so it must be to do with the scope of $DB?
Obviously I don’t want to have a new database connection for each class / query…
Also, do I need to worry about closing the connection at any point?
Many thanks in advance
Tim
It looks like you’re not informing PHP that $DB is a global variable. Try the following: