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

无法连接到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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料