Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过php连接到mysql_Php_Mysql - Fatal编程技术网

通过php连接到mysql

通过php连接到mysql,php,mysql,Php,Mysql,这是我的数据库连接类。它连接到mysql,但出于某种原因,它不想连接到数据库(我曾尝试在mysql\u select\u database中以字符串形式插入数据库名称,但仍然不起作用)。数据库存在,因为它是手动创建的。感谢您的帮助。检查您在MySQL上的访问权限。 是否允许用户使用给定的数据库?检查您在MySQL上的访问权限。 是否允许用户使用给定的数据库?您当前拥有: class DatabaseConnection{ private $connection; private $use

这是我的数据库连接类。它连接到mysql,但出于某种原因,它不想连接到数据库(我曾尝试在
mysql\u select\u database
中以字符串形式插入数据库名称,但仍然不起作用)。数据库存在,因为它是手动创建的。感谢您的帮助。

检查您在MySQL上的访问权限。
是否允许用户使用给定的数据库?

检查您在MySQL上的访问权限。 是否允许用户使用给定的数据库?

您当前拥有:

class DatabaseConnection{
  private $connection;
  private $username;
  private $password;
  private $serverName;
  private $database;
  private $database_found = false;

  //constructor with params
  public function __construct($database,$username,$password,$serverName){
    $this->$username = $username;
    $this->$password = $password;
    $this->$serverName = $serverName;
    $this->$database = $database;
  }

  //estabilish database connection
  public function connect()
  {
    $connection = mysql_connect($this->serverName,$this->username,$this->password);
    if($connection)
    {
      echo "Connection was successful!\n";
      $db = mysql_select_db($this->database,$connection);
      if($db)
      {
        echo "Database found!\n";
        $this->database_found = true;
      }
      else{
        echo "Database not found!\n";
      }
    }
    else {
      echo "Failed to connect to the database!\n";
    }
  }

  //closes connection
  public function close_connection(){
    mysql_close();
    echo "Connection closed!\n";
  }

  public function getConnection(){
    return $this->connection;
  }

  public function getDatabaseState(){
    return $this->database_found;
  }
}
这应该是:

$this->$username = $username;
您有4次出现这种情况。

您当前有:

class DatabaseConnection{
  private $connection;
  private $username;
  private $password;
  private $serverName;
  private $database;
  private $database_found = false;

  //constructor with params
  public function __construct($database,$username,$password,$serverName){
    $this->$username = $username;
    $this->$password = $password;
    $this->$serverName = $serverName;
    $this->$database = $database;
  }

  //estabilish database connection
  public function connect()
  {
    $connection = mysql_connect($this->serverName,$this->username,$this->password);
    if($connection)
    {
      echo "Connection was successful!\n";
      $db = mysql_select_db($this->database,$connection);
      if($db)
      {
        echo "Database found!\n";
        $this->database_found = true;
      }
      else{
        echo "Database not found!\n";
      }
    }
    else {
      echo "Failed to connect to the database!\n";
    }
  }

  //closes connection
  public function close_connection(){
    mysql_close();
    echo "Connection closed!\n";
  }

  public function getConnection(){
    return $this->connection;
  }

  public function getDatabaseState(){
    return $this->database_found;
  }
}
这应该是:

$this->$username = $username;

您有4次遇到这种情况。

mysql.*
函数已被弃用,您应该使用。这是我找到的第一本教程

您还可以在应该使用
$this->$username
的地方使用
$this->username
。这里是关于PHP类的

此外,约定是在类中以下划线开头编写私有变量,如下所示:

  • private$\u用户名来创建它
    
  • $this->\u用户名
    调用它

您还可以创建getter和setter来与变量交互。()

mysql.*
函数不推荐使用,您应该使用。这是我找到的第一本教程

您还可以在应该使用
$this->$username
的地方使用
$this->username
。这里是关于PHP类的

此外,约定是在类中以下划线开头编写私有变量,如下所示:

  • private$\u用户名来创建它
    
  • $this->\u用户名
    调用它

您还可以创建getter和setter来与变量交互。()

就像你说的那样,我得到了警告:mysql_connect():无法建立连接,因为目标计算机主动拒绝了它。在第22行的E:\xampp\htdocs\catchrecorder\database.php中,正如您所说,我得到了警告:mysql\u connect():无法建立连接,因为目标计算机主动拒绝了它。在E:\xampp\htdocs\catchrecorder\database.php第22行是的,我在本地机器上开发数据库是我创建的,我使用根帐户是的,我在本地机器上开发数据库是我创建的,我使用根帐户谢谢。我来看看!我知道OOP的实践:)只是使用了Java,但是对于这个项目,我更喜欢PHPThank。我来看看!我知道OOP的实践:)只是使用Java,但是对于这个项目,PHP对我来说更好