php配置文件,public function connect()错误。

时间:2022-02-20 17:07:37

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.

我是php的新手,我修改了一个关于如何创建博客的教程的代码,我必须在一个与mysql表相关的html表中填充一行。

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

现在我有了在公共函数connetti()中错误“解析错误:语法错误,意外T_PUBLIC”的配置文件。

    <?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...

也许我在声明公共函数时犯了一个致命的错误。但是因为这是我的第一个项目whit php我不明白问题在哪…

2 个解决方案

#1


2  

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.

我假设您希望函数在MysqlClass中定义,但是您试图在外部定义它们。很自然地,PHP不会让您和您意想不到的T_PUBLIC,因为它甚至意味着在全局范围内定义一个公共函数?访问修饰符只适用于类成员。

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.

将代码保持正确的缩进是帮助您捕获这些错误的好方法,并且还试图读取和理解错误。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; 
      }
    }
  }
}

#2


0  

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

在变量声明之后有一个额外的}。将其移动到函数声明的末尾(就在PHP结束标记之前)

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

(错误消息说,有些函数声明为public,但它们不是任何类的一部分。)

#1


2  

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.

我假设您希望函数在MysqlClass中定义,但是您试图在外部定义它们。很自然地,PHP不会让您和您意想不到的T_PUBLIC,因为它甚至意味着在全局范围内定义一个公共函数?访问修饰符只适用于类成员。

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.

将代码保持正确的缩进是帮助您捕获这些错误的好方法,并且还试图读取和理解错误。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; 
      }
    }
  }
}

#2


0  

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

在变量声明之后有一个额外的}。将其移动到函数声明的末尾(就在PHP结束标记之前)

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

(错误消息说,有些函数声明为public,但它们不是任何类的一部分。)