2

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?

Nikola Atanasov
  • 605
  • 1
  • 4
  • 11

3 Answers3

2

check your mysql credential....

You can reset the root password by running the server with --skip-grant-tables and logging in without a password by running the following as root (or with sudo):

# service mysql stop
# mysqld_safe --skip-grant-tables &
$ mysql -u root

mysql> use mysql;
mysql> update user set password=PASSWORD("YOUR-NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

# service mysql stop
# service mysql start
$ mysql -u root -p
Parth Chavda
  • 1,819
  • 1
  • 23
  • 30
1

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

Skyyy
  • 1,539
  • 2
  • 23
  • 60
  • I tried to edit privileges on phpmyadmin, but use root@localhost already had no password, so i deleted the password variables, nothing. I left the password field empty, still nothing. Next i tried including the config file, it says that the variables are already defined, and still gives the same errors. – Nikola Atanasov Nov 26 '15 at 10:00
  • Should i try connecting to a different database? Mby i did something wrong with this one? – Nikola Atanasov Nov 26 '15 at 10:01
  • Your wish . Try it if you get successful connection then its sure that its problem with login credentials not with your code – Skyyy Nov 26 '15 at 11:05
  • Also make sure user is assigned to `localhost` not to anyhost sometime that also causes problems – Skyyy Nov 26 '15 at 11:20
  • Just created a new database, testt, nothing in it, nothing changed, just linked it, it connected, and yes root user really had no password. I'll just make another DB since this one is empty, so i'll save us the trouble. Thank you for your trouble – Nikola Atanasov Nov 26 '15 at 16:04
0

You might be able to check if your credentials are correct by going to the console and typing:

mysql -uroot -p121212aa

If you are getting the same error it is likely to be credentials.

Matthew Spencer
  • 2,265
  • 1
  • 23
  • 28