i have this config file, related by a page (in a previous answer resolved).

i'm new to php and i've modified a code taken from a tutorial on how to create a blog, i must to fill some row in a html table related to a mysql table.

now i have the config file that make the error "Parse error: syntax error, unexpected T_PUBLIC" in public function connetti().

    <?php
class MysqlClass
{
  private $nomehost = "localhost";     
  private $nomeuser = "root";          
  private $password = "xxxx"; 
  private $nomedb = "intse";
  private $attiva = false;
 }
public function connetti()
 {
   if(!$this->attiva)
    {
     if($connessione = mysql_connect($this->nomehost,$this->nomeuser,$this->password) or die (mysql_error()))
      {
       $selezione = mysql_select_db($this->nomedb,$connessione) or die (mysql_error());
      }
     }else{
       return true;
     }
    } 
    public function disconnetti()
{
        if($this->attiva)
        {
                if(mysql_close())
                {
         $this->attiva = false; 
             return true; 
                }else{
                        return false; 
                }
        }
 }     
?>

Maybe i make a fatal error when declare the public function. but since this is my first project whit php i don't understand where is the problem...

有帮助吗?

解决方案

I assume you want your functions to be defined within your MysqlClass, but you're trying to define them outside. Naturally, PHP won't let you and gives you a unexpected T_PUBLIC, because what does it even mean to define a function public in the global scope? Access modifiers only apply to class members.

Keeping your code properly indented is a good way to help you catch these kinds of errors, and also trying to read and understand the error. PHP tells you expect what the problem is.

class MysqlClass
{
  private $nomehost = "localhost";     
  private $nomeuser = "root";          
  private $password = "xxxx"; 
  private $nomedb = "intse";
  private $attiva = false;

  public function connetti()
  {
    if(!$this->attiva)
    {
      if($connessione = mysql_connect($this->nomehost,$this->nomeuser,$this->password) or die (mysql_error()))
      {
        $selezione = mysql_select_db($this->nomedb,$connessione) or die (mysql_error());
      }
    } else{
      return true;
    }
  } 
  public function disconnetti()
  {
    if($this->attiva)
    {
      if(mysql_close())
      {
        $this->attiva = false; 
        return true; 
      } else {
         return false; 
      }
    }
  }
}

其他提示

You have an extra } after the variable declaration. Move it to the end of the function declarations (just before the PHP closing tag)

(The error message says that you have some functions declared as public, but they are not part of any class.)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top