Hi, im still pretty new to PHP and I have just started looking into classes / functions and OOP -

I have done loads of searching and watching tutorials and learning from others here on daniweb with great pleasure,

I was hoping that someone could fix the problem im having with a database function im trying to create.

database.php is as follows

<?php
require_once("Li_config.php");

class MySQLDatabase {

	private $connection;
	
	function __construct(){
		$this->open_connection();
	}
public function open_connection(){
	$this->$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	if(!$this->$connection){
		die("Database connection failed: " . mysql_error());
		} else{
		$db_select = mysql_select_db(DB_DATABASE, $this->connection);
			if (!$db_select){
				die("Databse selection failed: " . mysql_error());
		}			
	}

public function close_connection(){ //close connection to db
	if(isset($this->connection)){
	mysql_close($this->connection);
	unset($this->$connection);
		}
	}
	
public function query($sql){ //query dtabase
	$result = mysql_query($sql, $this->connection);
	$this->confirm_query($result);
		return $result;		
	}
	
	function mysql_prep( $value ) {
	$magic_quotes_active = get_magic_quotes_gpc();
	$new_enough_php = function_exists( "mysql_real_escape_string" ); //PHP >= v4.3.0
	if ( $new_enough_php ){ //PHP >= v4.3.0
		//undo any magic quote effects so mysql_real_escape_strind can do the work
		if ( $magic_quote_active ) { $value = stripslashes( $value );}
		$value = mysql_real_escape_string ( $value );
		}else{ //before php v4.3.0
		//if magic quotes arent already on then add slashes manually
		if ( !$magic_quotes_active ) { $value = addslashes ( $value ); }
		//if magic quotes are active, then the slashes already exist
	}
		return $value;
}

// database-neutral method mysql_fetch_array
public function fetch_array($result_set){
	return mysql_fetch_array($result_set);
	}

// database-neutral method mysql_num_rows
public function num_rows($result_set){
	return mysql_num_rows($result_set);
	}
	
// database-neutral method inset_id get the last id inserted on this current db connection
public function insert_id(){
	return mysql_insert_id($this->connection);
	}

// database-neutral affected rows
public function affected_rows(){
	return mysql_affected_rows($this->connection);
	}	
	
private function confirm_query($result){
if (!$result){
	die("Database query failed: " . mysql_error());
		}
	}
}
$database = new MySQLDatabase();
$connection =& $database;

?>

Lo_config.php contains my local database connection details.

<?php
$connection = mysql_connect('localhost', 'root', 'password') OR die("Error: ".mysql_error());
mysql_select_db('database', $connection) OR die("Error: ".mysql_error());

	define('DB_HOST', 'localhost');
    define('DB_USER', 'root');
    define('DB_PASSWORD', 'password');
    define('DB_DATABASE', 'database');
?>

when i run database.php i get the following error

Parse error: parse error in C:\xampp\htdocs\www.local.com\members\includes\database.php on line 22

I have tried removing the ( } ) from line 20 but Im still getting the same error message and im just a bit stumpped as where I am going wrong,

The brackets of the open_connection function don't match up.

Your code is difficult to read. I don't know if that is way it is, or it got mangled in the post.
I suspect that you have a misplaced curly bracket. I was trying to figure out if the class was properly terminated, but gave up due to the clutter.
Are youu using a code editor and/or IDE? They do bracket pairing, which is much easier than to find it amidst a bunch of code.

Member Avatar for rajarajan2017
public function open_connection(){
	$this->$connection = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	if(!$this->$connection){
		die("Database connection failed: " . mysql_error());
		} else{
		$db_select = mysql_select_db(DB_DATABASE, $this->connection);
			if (!$db_select){
				die("Databse selection failed: " . mysql_error());
		}			
	}
}

Missing the closing brace

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.