drmy1050 2015-11-26 04:39
浏览 197
已采纳

无法连接到Database-MySQL

I know this question has been asked many times, but in all other posts i searched, i just couldn't find the answer. I just cant seem to connect to the database.

My OS is Ubuntu 12.04, if it helps.

Here is my code for config.php:

<?php
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','121212aa');
define('DB_NAME','PHP-Wizard');
?>

And also Database.php

<?php
class Database{
public $host = DB_HOST;
public $username = DB_USER;
public $password = DB_PASS;
public $db_name = DB_NAME;

public $link;
public $error;

/*
 * Class Constructor
 */
public function __construct(){
    //Call Connect Function
    $this->connect();
}

/*
 * Connector
 */
 private function connect(){
    $this->link = new mysqli($this->host, $this->username, $this->password, $this->db_name);

    if(!$this->link){
        $this->error = "Connection Failed: ".$this->link->connect_error;
        return false;
    }
 }

 /*
  * Select
  */
  public function select($query){
    $result = $this->link->query($query) or die($this->link->error.__LINE__);
    if($result->num_rows > 0){
        return $result;
    } else {
        return false;
    }
  }

  /*
   * Insert
   */
   public function insert($query){
        $insert_row = $this->link->query($query) or die($this->link->error.__LINE__);

        //Validate Insert
        if($insert_row){
            header("Location: index.php?msg=".urlencode('Record Added'));
            exit();
        } else {
            die('Error : ('. $this->link->errno .') '. $this->link->error);
        }
   }

   /*
   * Update
   */
   public function update($query){
        $update_row = $this->link->query($query) or die($this->link->error.__LINE__);

        //Validate Insert
        if($update_row){
            header("Location: index.php?msg=".urlencode('Record Updated'));
            exit();
        } else {
            die('Error : ('. $this->link->errno .') '. $this->link->error);
        }
   }

    /*
   * Delete
   */
   public function delete($query){
        $delete_row = $this->link->query($query) or die($this->link->error.__LINE__);

        //Validate Insert
        if($delete_row){
            header("Location: index.php?msg=".urlencode('Record Deleted'));
            exit();
        } else {
            die('Error : ('. $this->link->errno .') '. $this->link->error);
        }
   }

And here is the error i get when i try to open index.php

Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'root'@'localhost' (using password: YES) in /opt/lampp/htdocs/Forum/Database.php on line 23

Warning: mysqli::query(): Couldn't fetch mysqli in /opt/lampp/htdocs/Forum/Database.php on line 35

Warning: Database::select(): Couldn't fetch mysqli in /opt/lampp/htdocs/Forum/Database.php on line 35

Any answers as to why i cant connect to the Database?

  • 写回答

3条回答 默认 最新

  • duanba5777 2015-11-26 06:10
    关注

    Possible reasons are your database credentials are wrong. If you don't remember your password you can easily change. As you mentioned in one of comments (could reinstalling xampp fix it). Well it won't Instead try changing password

    Visit

    http://localhost/phpmyadmin
    //Then visit
    User > Select user and click edit privileges > Login Information
    

    Here you can overwrite old password with new one

    If you are on remote server (Cpanel) you can reset your password from database options

    Also i don't see anywhere in your code including config.php make sure its including in you class file

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog