0

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.

Edited by Roebuc: spelling

3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by Roebuc
0

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();
  }
}
0

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!

0

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.

1

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.

Votes + Comments
Thanks for the info!
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.