0

I want a generic php file with all the database info (dbname,host,username,password)

But when I include the page, in like index.php I get this error:

Access denied for user 'apache'@'localhost' (using password: NO)

connect.php

<?php
class dbconnect{
    private $host = '**'; 
    private $user = '**';
    private $pass = '**';
    public $con;

function Connect($db = '**') {

    if($db=='**'){
        $this->host="localhost";
        $this->user="**";
        $this->pass="**";
        $db="**";
    }else{
                $this->host="**";
                $this->user="**";
                $this->pass="**";
    }

    $this->con = mysql_connect($this->host,$this->user,$this->pass);
    if (!$this->con)
      {
        die('Could not connect: ' . mysql_error());
      }
    $blaa = mysql_select_db($db, $this->con);
    mysql_query("SET NAMES UTF8");
    return $blaa;
}

function Disconnect() {
    //$this->con = mysql_connect($this->host,$this->user,$this->pass);
    mysql_close();
}
}
?>

I am sure the ** information is correct because when I specify it as:

$con=mysqli_connect("example.com","example","password","my_db");

In index.php it works

4 Answers 4

1

It's important to note, your test case doesn't actually prove it works.

What does this output:

$conn = mysql_connect("example.com", "user", "password");
if (!$conn) {
    die('Could not connect: ' . mysql_error());
}

As you won't necessarily get the information that's failing it without that.

To top that off, let's simplify your class a little bit for debugging purposes:

class dbconnect
{
    private $host = '**';
    private $user = '**';
    private $pass = '**';
    public $con;

    public function Connect($host = "localhost", $user = "root", $pass = "")
    {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;

        $this->con = mysql_connect($this->host, $this->user, $this->pass);
        if (!$this->con) {
            die('Could not connect: ' . mysql_error());
        }

        $blaa = mysql_select_db($db, $this->con);
        mysql_query("SET NAMES UTF8");

        return $blaa;
    }

    public function Disconnect()
    {
        mysql_close($this->con);
    }
}

Now what do you get when you do

$db = new dbconnect("example.com", "user", "password");

Be sure you're using credentials that work, and that you're not running into issues such as default values or incorrect variable assignment through these methods.

Now, if you don't want to provide the values, you can simply:

$db = new dbconnect();

Public Service Announcement

Check out PHP's PDO or at minimum (but really, just use PDO) the mysqli alternative. PHP's mysql extension is NOT secure, and you should not be using it in any environment, ever.

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

Comments

0

If the connection information are correct, check your MySQL User's host access permission:

SELECT user, host FROM mysql.user

If "localhost" is set, then the user can only access the database locally, otherwise "%" will open access.

Comments

0

Usually it is an issue with the connection credentials.

Check that you can log into your mysql using the details you have set for that website.

mysql -u apache -p

It will then ask you for the password.

If that login does not work, then you have a problem with that user's mysql account.

Comments

0

You use incorrect parameters for accessing. Just dump variables which at the line $this->con = mysql_connect($this->host,$this->user,$this->pass);. You can use debugger or echo, print instructions.

Furthermore use PDO extension for accessing to databases. It's better! Why shouldn't I use mysql_* functions in PHP?

3 Comments

Are you sure that you want to revive a question that is more than five years old? ;)
Ooops. Sorry guy:)
Dangit. Didn't realize this was a revival, and I totally wrote out a response. Haha, whelp.

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.