Hello,
I was wondering if it's possible to call functions that are external to a class?
for example lets say I have this class:
class AjaxDropdown
{
var $table;
function AjaxDropdown()
{
// Make the connnection and then select the database.
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
$this->table = "tblcategories";
}
function dbConnect()
{
DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
}
function getXML($id)
{
$this->dbConnect();
$query = "SELECT * FROM $this->table where pid = {$id} ORDER BY id asc";
$result = mysql_db_query (DB_NAME, $query, LINK);
$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?>';
$xml .= '<categories>';
while($row = mysql_fetch_array($result))
{
$xml .= '<category>';
$xml .= '<id>'. $row['id'] .'</id>';
$xml .= '<fname>'. $row['Name'] .'</fname>';
$xml .= '</category>';
}
$xml .= '</categories>';
mysql_close();
return $xml;
}
}
However I already have writen a db.inc.php file which handles the connections such as this.
CLASS SQLCon
{
VAR $host, $sock, $port, $user, $passwd, $db, $connected, $link, $msgttl, $msgbdy;
FUNCTION connect() {
$Sleep = 1000; #uSeconds
$ConAttempts = 1500;
IF ( $this->connected )
RETURN;
IF ( strlen( $this->port ) ) $this->port = ":".$this->port;
IF ( strlen( $this->sock ) ) $this->sock = ":".$this->sock;
IF ( !DEFINED( 'DB_VERM' ) OR DB_VERM == 0 ) {
# ( @ ) DO NOT Display All VERM ( NORMAL Mode ) Normal Connection surpesses all warnings / errors
FOR ( $i = 0; $i < $ConAttempts; $i++ ) {
IF ( DB_PCON AND !$this->link = @mysql_pconnect( $this->host . $this->port . $this->sock, $this->user, $this->passwd ) ) {
usleep( $Sleep ); #Not available on windows before PHP 5.0
}ELSEIF( !DB_PCON AND !$this->link = @mysql_connect( $this->host . $this->port . $this->sock, $this->user, $this->passwd ) ) {
usleep( $Sleep ); #Not available on windows before PHP 5.0
}ELSE{
BREAK;
}
}#[ END ] 1st FOR ( $i = 0; $i < DB_ATTEMPTS; $i++ )
}ELSE{
# No ( @ ) Display All VERM ( DEBUG Mode ) Do not surpress warnings / errors
FOR ( $i = 0; $i < $ConAttempts; $i++ ) {
IF ( DB_PCON AND !$this->link = mysql_pconnect( $this->host . $this->port . $this->sock, $this->user, $this->passwd ) ) {
usleep( $Sleep ); #Not available on windows before PHP 5.0
}ELSEIF( !DB_PCON AND !$this->link = mysql_connect( $this->host . $this->port . $this->sock, $this->user, $this->passwd ) ) {
usleep( $Sleep ); #Not available on windows before PHP 5.0
}ELSE{
BREAK;
}
}#[ END ] 2nd FOR ( $i = 0; $i < DB_ATTEMPTS; $i++ )
}#[ END ] IF ( !DEFINED( 'DB_VERM' ) OR DB_VERM == 0 )
IF ( $this->link ) {
$this->connected = TRUE;
}ELSE{
$this->connected = FALSE;
$out = "";
$out .= "<table align=center border=\"1\" cellpadding=\"2\" cellspacing=\"2\" style=\"border-color:$err_color\"><tr><td bgcolor=\"#9C21A5\">";
$out .= "<font color=white face=\"Arial, Helvetica, sans-serif\"><b>";
$out .= $this->msgttl;
$out .= "</b></font></td></tr>";
$out .= "<tr><td>";
$out .= $this->msgbdy;
$out .= "</td></tr></table>";
ECHO $out;
EXIT();
}
}# [ END ] FUNCTION connect()
FUNCTION select_db() {
IF ( !DEFINED( 'DB_VERM' ) OR DB_VERM == 0 )
RETURN @mysql_select_db( $this->db );# No ( @ ) Display All VERM ( DEBUG Mode )
ELSE RETURN mysql_select_db( $this->db ); # ( @ ) DO NOT Display All VERM ( NORMAL Mode )
}# [ END ] FUNCTION select_db ()
}
$MySQL = new SQLCon;
$MySQL->host = $db['host'];
$MySQL->sock = $db['sock'];
$MySQL->port = $db['port'];
$MySQL->user = $db['user'];
$MySQL->passwd = $db['passwd'];
$MySQL->db = $db['db'];
$MySQL->msgttl = $db['msgttl'];
$MySQL->msgbdy = $db['msgbdy'];
$MySQL->connected = FALSE;
$MySQL->connect();
$MySQL->select_db();
/**
* db_connect() Connect to data base
**/
FUNCTION db_connect()
{
GLOBAL $MySQL;
$Code = 100;
IF ( !$MySQL->select_db() ) {
$SqlError = $_SERVER["PHP_SELF"]." : ". mysql_error();
$Msg = dbErrorMessage( $Code, "Database connection failed Error Code", $SqlError , "db_connect()" );
IF ( !DEFINED( 'DB_VERM' ) OR DB_VERM == 0 ) {# Only send mail when not in debug mode
//mail( BUG_REPORT_EMAIL, "Error ". $Code, "Error in $_SERVER[PHP_SELF] : " . $SqlError );
dbPostOffice( $Msg, "dBase Error Report" );
ECHO dbPrintError( $Msg , "MemberPageDisplay");
}
IF ( DB_VERM == 1 ) {
ECHO dbPrintError( $Msg , "VERM");
}
EXIT();
}
RETURN TRUE;
}
FUNCTION dbRes( $Query, $Display = FALSE ) {
GLOBAL $MySQL;
$Name = "dbRes";
$Code = 103;
$n = 0;
IF ( !DEFINED( 'DB_VERM' ) OR DB_VERM == 0 AND !$Display )
$Res = @mysql_query( $Query, $MySQL->link );
ELSE $Res = mysql_query( $Query, $MySQL->link );
IF ( !$Res AND !$Display ) {
$SqlError = $_SERVER["PHP_SELF"]." : ". mysql_error();
$Msg = dbErrorMessage( $Code, $Query, $SqlError , "dbRes" );
IF ( !DEFINED( 'DB_VERM' ) OR DB_VERM == 0 ) {# Only send mail when not in debug mode
dbPostOffice( $Msg, "dBase Error Report" );
dbPrintError( $Msg , "MemberPageDisplay");
}
IF ( DB_VERM == 1 ) {
ECHO dbPrintError( $Msg , "VERM");
}
EXIT();
}
IF ( DEFINED('DB_VERM') AND DB_VERM == 2 OR $Display ) {
$Msg = dbErrorMessage( $Code, $Query, "DB_VERM = TRUE in header.inc.php" , "dbRes" );
ECHO dbPrintError( $Msg , "VERM");
IF ( $Display ) {
ECHO "<br><br>Query Display = " . $Query;
WHILE( $Arr = mysql_fetch_array( $Res ) ) {
ECHO "<br>db Result ". $n ." = ";
PRINT_R( $Arr );
$n++;
}
}
}
RETURN $Res;
}
Now when I do a simple query in my scripts I just do
$Result = dbRes("blabla");
Is there a way to replace the queries in the AjaxDropdown class such as
$result = mysql_db_query (DB_NAME, $query, LINK);
Replaced with
$result = dbRes ( $query );