can someone tell me what is wrong with the following:

function connect($db_host,$db_database,$db_user,$db_pass){
	
		global $db_connection;
	
		try {
			
			$db_connection = mysql_connect($DB_HOST,$db_user,$db_pass, true);
			
			if(!$db_connection) {
				throw new Exception('MYQSL Connection Database Error: ' . mysql_error());
			} else {
				$connection = true;
			}
			
		catch (Exception $e) {
			echo $e->GetMessage();
		}
	}

I receive:

Parse error: syntax error, unexpected '{'

Refering to the { after try.

Thanks.

Recommended Answers

You forgot a } before "catch". Try:

function connect($db_host,$db_database,$db_user,$db_pass) {
global $db_connection;
try {
  $db_connection = mysql_connect($DB_HOST,$db_user,$db_pass, true);
  if(!$db_connection) {
    throw new Exception('MYQSL Connection Database Error: ' . mysql_error());
    }
  else {
    $connection = true;
    }
  }
catch (Exception $e) {
  echo $e->GetMessage();
  }
}
Jump to Post

I ran your code through my interpreter and I didn't get an error. Is it possible that you are running on PHP 4? I believe that try and catch are PHP 5 only.

Jump to Post

All 5 Replies

You forgot a } before "catch". Try:

function connect($db_host,$db_database,$db_user,$db_pass) {
global $db_connection;
try {
  $db_connection = mysql_connect($DB_HOST,$db_user,$db_pass, true);
  if(!$db_connection) {
    throw new Exception('MYQSL Connection Database Error: ' . mysql_error());
    }
  else {
    $connection = true;
    }
  }
catch (Exception $e) {
  echo $e->GetMessage();
  }
}

Thank you for the reply. That is definitely an oversight on my part :D I will test that when I get home, however, the syntax error was pointing to the line that contained:

try {

Obviously that is a mistake on my part that I need to fix, however, I think that would be a second syntax error. I will fix it when I get home and update. Thanks for the reply!

Unfortunately, that did not work. I made a few changes to the code but still receiving the same error:

function connect(){
	
		global $db_connection;
	
		try {
			
			$this->db_connection = mysql_connect($DB_HOST,$this->db_user,$this->db_pass, true);
			
			if(!$this->db_connection) {
				throw new Exception('MYQSL Connection Database Error: ' . mysql_error());
			} else {
				$this->connection = true;
			}
			
		} catch (Exception $e) {
			echo $e->GetMessage();
		}
	}

This error makes me think there is something in a function defined earlier. However, if I remove the try and catch statement, it works fine. Thanks for the help.

I ran your code through my interpreter and I didn't get an error. Is it possible that you are running on PHP 4? I believe that try and catch are PHP 5 only.

commented: Thanks for the info! +2

Sorry for the long reply time. Just found that I am using PHP 4. Thanks for the info, I will see if I can get my hosting company to upgrade my php. Thanks.

Be a part of the DaniWeb community

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