0

From a friend, I got a website (zip file) with PHP and HTML files in it. I also got a SQL script from him. Now what I'm trying to do is run the application on localhost (Xampp apache server) and the website does load, but it looks like I can't connect to the database.(I did import the SQL script and it worked, But I'm still getting this error) This is an error for example that I'm receiving on the home page:

Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'dewaai'@'localhost' (using password: YES)
Notice: Undefined index: logged in C:\xampp\htdocs\deWaai\includes\header.php on line 14

The name of the database is 'dewaai' The username is: root And I think there is no password...

This is the connection.php file:

<?php
session_start();

include "db.php";

$db = new db();
$db->setDB("localhost", "dewaai", "dewaai", "dewaai");
$db->connect();

?>

These are database functions in the db.php file:

<?php
class db {
    private $conn;
    private $servername;
    private $dbuser;
    private $dbpass;
    private $dbname;

    function setDB($servername, $dbuser, $dbpass, $dbname) {
        $this->servername = $servername;
        $this->dbuser = $dbuser;
        $this->dbpass = $dbpass;
        $this->dbname = $dbname;
    }

    function connect() {
        try {
            $this->conn = new PDO("mysql:host=$this->servername;dbname=$this->dbname", $this->dbuser, $this->dbpass);
            // set the PDO error mode to exception
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $e)
        {
            echo "Connection failed: " . $e->getMessage();
        }
    }

Does anyone know how I can fix this so the database runs the application? I really don't know why I'm getting that error. Any kind of help is appreciated, thx

5
  • "I think there is no password.", I doubt that. Root user and no password? Commented Apr 23, 2018 at 9:48
  • How can I check if there is set a password? Commented Apr 23, 2018 at 9:48
  • Don't use the root user. Explicitly define a user and a password for MySQL. Commented Apr 23, 2018 at 9:49
  • @KIKOSoftware it's not that unusual to have a [email protected] user with no password. caveat being that it only accepts logins from 127.0.0.1. i've seen that several times (it makes the db easier to hack into, if an hacker can find a remote code execution vulnerability somewhere, but still) Commented Apr 23, 2018 at 9:51
  • I think issue in this line can you check commas here mysql:host=$this->servername;dbname=$this->dbname", $this->dbuser, $this->dbpass Commented Apr 23, 2018 at 9:55

2 Answers 2

1

There is problem with your username or password. $db->setDB("localhost", "root", "", "dewaai"); Try this it will help you to connect with your database

Sign up to request clarification or add additional context in comments.

Comments

0

Hi the issue clearly from the error message states that your username and password given in connection.php file is incorrect.

The format for DB connection is $db->setDB( Database host IP address or DNS name , Username , Password, DatabaseName);

From what you said i guess connection.php file should be

<?php
session_start();

include "db.php";

$db = new db();
$db->setDB("localhost", "root", "", "dewaai");
$db->connect();

?>

And i think your PDO connection might have an issue as well. It should be

$this->conn = new PDO('mysql:host='.$this->servername.'; dbname='.$this->dbname, $this->dbuser, $this->dbpass);

Hope this helps.

2 Comments

Concatenating the string like the OP does it is valid since it's in double quotes.
Ah yeah, you are right. Didnt know that. Was used to doing it this way. Thanks for pointing that out @Magnus Eriksson. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.