Hello guys!
I am trying to figure out how to connect to a database from a function that is within a class. There is a class that has a connection function defined and here is the code for it.
[code=php]
class TDatabase {
var $host;
var $user;
var $passwd;
var $db;
var $hLink;
var $isConnected; // boolean to check if the connection is active
var $isPConnected; // boolean to check if the connection is persistent or not when the connection is active
// the field $hDB has been killed because it has no role
// I would like to add a constructor with $host, $user, $passwd and $db as
// parameters and to not add set and get methods for these fields.
function TDatabase($host, $user, $passwd, $db) {
$this->host = $host;
$this->user = $user;
$this->passwd = $passwd;
$this->db = $db;
$this->isConnected = false;
}
function connect() {
// for non persistent connections the best choice in many case
if (!$this->isConnected) {
$this->hLink = @mysql_connect($this->host, $this->user, $this->passwd);
if (($this->hLink) && (@mysql_select_db($this->db, $this->hLink))) {
$this->isConnected = true; // the connection is active
$this->isPConnected = false; // this is a non persistent connection
return true; // the connection is successfull and the database is selected
}
else
return false; // an error has occured
}
else
return false; // already connected
}
function output() {
echo “Database info: “;
echo “hostname=$host, user=$user, passwd=$passwd,db_name=$dbn”;
if ($isConnected)
echo “,connected to server”;
if ($isPConnected)
echo “,persistent connection”;
echo “n”;
}
}
No I use that within a file that I will include into another for my use. Now I tried calling up the function in another page such as:
[code=php]
<?php
include “incl.php”;
$connect();
$output();
?>
but no luck, I get an error that says “Call to undefined function: ()” Now do I have to state the class and point it to the function? Any ideas or hints are welcomed! Thanks! ?