Right well i thought that seeing as i am creating a complete application i thought i would write my own db class just done a little bit of it and i am getting a nice error of

Fatal error: Call to undefined function mysql_connect() in *:\****************\database.engine.php on line 61

it is proberbly a nice and easy error to work out but i am kinda brain dead atm so....

<?php
/*
 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +++           Acid Avengers Software            +++
 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +++            Product: Acid Forum              +++
 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +++ Website: http://www.acidavengers.co.uk      +++
 +++ E-Mail:  support@acidavengers.co.uk         +++
 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +++ Created By: Marc "Acid Burn" Towler         +++
 +++ Copyright 2006 - 2007 Acid Avengers         +++
 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +++       File Name: database.engine.php        +++
 +++       File Version: 0.1                     +++
 +++++++++++++++++++++++++++++++++++++++++++++++++++

 _______________________________________________________________________
/***********************************************************************\
|***                        File Description                         ***|
|***********************************************************************|
|*** This class servers as an interim between the PHP system and a   ***|
|*** database protocol. Initially this class will only handle MySQL, ***|
|*** MySQLi and SQLite, the reason for SQLite support at an inital   ***|
|*** stage is to allow a backup system to be implimented from the    ***|
|*** start. Later support will be given to PDO, PEAR DB and pgSQL.   ***|
|*** Oracle and MSSQL will be supported optionally at a later date   ***|
|***********************************************************************|
\_______________________________________________________________________/
*/

class databaseEngine {

    //Construct obtains and then connects to the database
	function __construct($host = 'localhost', $user = '', $pass = '', $port = 3306, $db = '')
	{
	    //check to see that everything is present, if not show error
		if($user == '' || $pass == '' || $db == '')
		{
		    $this->error    = 1;    //set the error flag
			$this->errormsg = 'Database unable to connect, insufficent details';
			$this->errorno  = 0001; //set to a non-mysql error number
			
			return false;
		}
		
		//Must be ok so lets connect
		$this->dbConnect($host, $user, $pass, $port, $db);
	}

    //Destruct closes and cleans up any MySQL bits
	function __destruct()
	{
	}

    //dbConnect function uses mysql_connect
	function dbConnect($host, $user, $pass, $port, $db)
	{
	    if($port == 3306)
		{
		    $return = mysql_connect($host, $user, $pass);
			
			if($return)
			{
			    return $this->selectDB($db);
			}
		} else {
		
		    //Not the standard port so need to use the format host:port
		    $host = $host . ":" . $port;
			
			$return = mysql_connect($host, $user, $pass);
			
			if($return)
			{
    			return $this->selectDB($db);
			}
		}
	}

    //selectDB function uses mysql_select_db
	function selectDB($db)
	{
	    $result = mysql_select_db($db);
		
		return $result;
	}

    //query function uses mysql_query
	function query($sql)
	{
	    $result = mysql_query($sql);
		
		return $result;
	}

    //fetchArray function uses mysql_fetch_array
	function fetchArray()
	{
	}

    //free function uses mysql_free_result to free the resultset
	function free()
	{
	}

    //affectedRows function uses mysql_affected_rows
	function affectedRows()
	{
	}

    //pConnect uses mysql_pconnect to make a persistant connection
	function pConnect()
	{
	}

    //error function checks, logs and if set shows errors in a friendly manner
	function error()
	{
	}
}
?>

Recommended Answers

All 4 Replies

umm.. Are you using php 5+ on apache 2.0+ ? By default, mysql service is disabled in php5. You need to add 2 lines in httpd.conf file in apache/conf.

LoadModule php5_module php5apache2.dll
AddType application/x-httpd-php .php

Open php.ini and uncomment mysql extension. Check the extension_dir folder in php.ini. Specify the right path of ext directory in php. ( eg. c:/php/ext). Copy libmysql.dll from php folder to system32 folder. Thats it. Restart apache.

Cheers,
Naveen

Done all of that when i set the server up a while ago nav33n thanks for the idea tho

UPDATE: i re-wrote the code for the database engine, i am now getting this error

Fatal error: Cannot re-assign $this in C:\Inetpub\wwwroot\forums\system\engines\database.engine.php on line 261

here is the new code:

<?php
/*
 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +++           Acid Avengers Software            +++
 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +++            Product: Acid Forum              +++
 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +++ Website: http://www.acidavengers.co.uk      +++
 +++ E-Mail:  support@acidavengers.co.uk         +++
 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +++ Created By: Marc "Acid Burn" Towler         +++
 +++ Copyright 2006 - 2007 Acid Avengers         +++
 +++++++++++++++++++++++++++++++++++++++++++++++++++
 +++       File Name: database.engine.php        +++
 +++       File Version: 0.1                     +++
 +++++++++++++++++++++++++++++++++++++++++++++++++++

 _______________________________________________________________________
/***********************************************************************\
|***                        File Description                         ***|
|***********************************************************************|
|*** This class servers as an interim between the PHP system and a   ***|
|*** database protocol. Initially this class will only handle MySQL, ***|
|*** MySQLi and SQLite, the reason for SQLite support at an inital   ***|
|*** stage is to allow a backup system to be implimented from the    ***|
|*** start. Later support will be given to PDO, PEAR DB and pgSQL.   ***|
|*** Oracle and MSSQL will be supported optionally at a later date   ***|
|***********************************************************************|
\_______________________________________________________________________/
*/

class databaseEngine {

     //Declare some of the class variables
	 var $conID;		//this will help later for the pcon ;)
	 var $log;			//Logs the process of what was called, any probs etc, helps to track probs down
	 var $dbType;
	 var $db;
	 var $server;
	 var $con;			//The current connection
	 var $error;		//Error text
	 var $errno;		//Error Number
	 var $lastID;		//Used for insert
	 var $affectedRows;	//Number of affected rows from insert, update and delete
	 var $timer;		//The start time, in miliseconds.

    //Construct obtains and then connects to the database
	function __construct($conid = 0)
	{
		//Set the connection ID
		$this->timer = $this->getMicroTime();
	    $this->conID = $conid;
		$this->log   = 'Database class initialised<br />';
		$this->errno = 0;	//Make sure there is no error number
		$this->error = '';  //Make sure there is no text
	}

    //Destruct closes and cleans up any MySQL bits
	function __destruct()
	{
	}

    /*
	This function connects to one of the supported database types, enter your credentials in the 
	suitable part of the switch statement below. This is part of the private functions but is here
	due to the fact you need to enter details in it
	*/
	function dbConnect()
	{
	    //Make the log entry
		$this->log .= 'dbConnect() called<br />';
		
		//switch statment to choose which db type
		switch($this->conID)
		{
		    case 0:
			    $this->dbType = 'MySQL';
				$this->server = 'localhost';
				$username     = 'acid';
				$password     = '******';
				$this->db     = 'test';
				break;
				
			case 1:
			    $this->dbType = 'MySQLi';
				$this->server = '';
				$username     = '';
				$password     = '';
				$this->db     = '';
				break;
				
			case 2:
			    $this->dbType = 'SQLite';
				$this->server = '';
				$username     = '';
				$password     = '';
				$this->db     = '';
				break;
				
			case 3:
			    $this->dbType = 'PDO';
				$this->server = '';
				$username     = '';
				$password     = '';
				$this->db     = '';
				break;
				
			case 4:
			    $this->dbType = 'MSSQL';
				$this->server = '';
				$username     = '';
				$password     = '';
				$this->db     = '';
				break;
			
			case 5:
			    $this->dbType = 'PostgreSQL';
				$this->server = '';
				$username     = '';
				$password     = '';
				$this->db     = '';
				break;
			
			case 6:
				$this->dbType = 'PearDB';
				$this->server = '';
				$username     = '';
				$password     = '';
				$this->db     = '';
				break;
		}

        //Lets connect to the right db and then select the database
		switch($this->dbType)
		{
		    case 'MySQL':
			    if(!$this->con = mysql_connect($this->server, $username, $password))
				{
				    $this->log .= 'Unable to connect to the MySQL server<br />';
					$this->log .= mysql_errno() . ': ' . mysql_error() . '<br />';
					return false;
				}
				
				if(!mysql_select_db($this->db, $this->con))
				{
				    $this->log .= 'Unable to connect to the database:' . $this->db . '<br />';
					$this->log .= mysql_errno() . ': ' . mysql_error() . '<br />';
					return false;
				}
				
				break;
			
			case 'MySQLi':
				if(!$this->con = mysqli_connect($this->server, $username, $password))
				{
				    $this->log .= 'Unable to connect to the MySQL server using MySQLi<br />';
					$this->log .= mysqli_errno() . ': ' . mysqli_error() . '<br />';
					return false;
				}
				
				if(!mysqli_select_db($this->con, $this->db))
				{
				    $this->log .= 'Unable to connect to database:' . $this->db . '<br />';
					$this->log .= mysqli_errno() . ': ' . mysqli_error() . '<br />';
					return false;
				}
				
				break;
			
			case 'PDO':
				if(!$this->con = new PDO('mysql:host=' . $this->server . ';dbname=' . $this->db, $username, $password))
				{
				    $this->log .= 'Unable to connect to the MySQL server/database using PDO (' . $this->db . '<br />';
					$this->log .= mysql_errno() . ': ' . mysql_error() . '<br />';
					return false;
				}
				
				break;
			
			case 'MSSQL':
			    if(!$this->con = mssql_connect($this->server, $username, $password))
				{
				    $this->log .= 'Unable to connect to the MSSQL server<br />';
					$this->log .= 'MSSQL returned the following message: ' . mssql_get_last_message() . '<br />';
					return false;
				}
				
				if(!mssql_select_db($this->db, $this->con))
				{
				    $this->log .= 'Unable to connect to database: ' . $this->db . '<br />';
					$this->log .= 'MSSQL returned the following message: ' . mssql_get_last_message() . '<br />';
					return false;
				}
				
				break;
			
			case 'PostgreSQL':
			    if(!$this->con = pg_connect('host=' . $this->server . 'dbname=' . $this->db . 
				   'user=' . $username . 'password=' . $password))
				{
				    $this->log .= 'Unable to connect to the PostgreSQL server<br />';
					$this->log .= 'PostgreSQL left the following message: ' . pg_last_error() . '<br />';
					return false;
				}
				
				break;
			
			//SQLite is to be used as a backup database server, not as a live one unless the main db server is down
			case 'SQLite':
			    if(!$this->con = sqlite_open($this->db, 0666, $this->error))
				{
				    $this->log .= 'Unable to connect to SQLite file<br />';
					$this->log .= 'SQLite returned the following message: ' . $this->error . '<br />';
					return false;
				}
				
				break;
			
			case 'PearDB':
			    if(!$this->con = DB::connect('mysql://' . $username . ':' . $password . '@' . $this->server . '/' . $this->db))
				{
				    $this->log .= 'Unable to connect to MySQL server using PEAR DB<br />';
					$this->log .= $this->con->getMessage() . '<br />';
					return false;
				}
				
				break;
		}
		
		//Log the fact that the function was successful
		$this->log .= 'dbConnect() executed successfully<br />'; 
		return true;
	}

    //query function uses mysql_query
	function query($sql)
	{
	}

    //fetchArray function uses mysql_fetch_array
	function fetchArray()
	{
	}

    //free function uses mysql_free_result to free the resultset
	function free()
	{
	}

    //affectedRows function uses mysql_affected_rows
	function affectedRows()
	{
	}

    //pConnect uses mysql_pconnect to make a persistant connection
	function pConnect()
	{
	}

    //error function checks, logs and if set shows errors in a friendly manner
	function error($errno, $error)
	{
	    $this->log .= 'error() called<br />';
		
		if($errnum == '' && $errtext != '')
		{
		    //Whatever called it doesn't use an error number/ an error number didn't pass properly
			$errnum = 1;
		}
		
		if($errnum == '' && $errtext == '')
		{
		    $this->log .= 'Exiting error(): No parameters passed<br />';
			return false;
		}		
	}
	
	//Function to get the current time
	//@return The current time in seconds with microseconds (in float format).
	function getMicroTime()
	{
        list($msec, $sec) = explode(' ', microtime());
        return floor($sec / 1000) + $msec;
    }
    /** Get how many time the script took from the begin of this object.
      * @return The script execution time in seconds since the
      * creation of this object.
      */
    function getExecTime()
    {
      return round(($this->getMicroTime() - $this->timer) * 1000) / 1000);
    }
}
?>

I see an error in this part of the code.

function getExecTime()
    {
      return round(($this->getMicroTime() - $this->timer) * 1000) / 1000);
    }

Another ) is missing in the return. Check if that solves your problem.

I noticed that but it doesnt help, i am totally stumped on the error message that php has given, as far as i know $this hasnt been reassigned

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.