Jump to content

Class is not working correctly!


marcus.whybrow

Recommended Posts

Hi, I have constructed this class for handeling making connections to a mysql database, but I can understand why this will not work, it allways returns the error:

 

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in C:\Inetpub\wwwroot\scriptsoft.co.uk\cpt\index.php on line 18

 

and this is the code:

 

<?php

class Connection {

var $result;

 

function Connection($localhost, $username, $password, $database){

$con = mysql_connect($localhost,$username,$password);

mysql_select_db($database, $con);

if($con)

{

$this->result = 'successful connection';

}

else

{

$this->result = 'failed connection';

}

}

 

$test = &new Connection('localhost','***username(removed)***','***password(removed)***','cpt');    //line 18

echo $test->result;

?>

 

Any help would be greatly apprieciated. Thanks,

 

Marcus

Link to comment
https://forums.phpfreaks.com/topic/55652-class-is-not-working-correctly/
Share on other sites

<?php
   class Connection {
   var $result;
   
   function Connection($localhost, $username, $password, $database){
      $con = mysql_connect($localhost,$username,$password);
      mysql_select_db($database, $con);
      if($con)
      {
         $this->result = 'successful connection';
      }
      else
      {
         $this->result = 'failed connection';
      }
   }
} // Add curly bracket here
   $test = &new Connection('localhost','***username(removed)***','***password(removed)***','cpt');     //line 18
   echo $test->result;
?>

 

You never close your brackets for your Connection class (see code for added curly bracket)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.