Jump to content

Log in error MySQL/MariaDB


lewashby
Go to solution Solved by maxxd,

Recommended Posts

test.php

<?php


include('./database.php');


$connection = new Connection();


?>

database.php

<?php


include('./constants.php');


class Connection
{
    function __construct()
    {
        try
        {
            $db_connection = new PDO("mysql:host=$host; dbname=$db_name", $db_user, $password);


            if($db_connection)
            {
                $db_connections = self::return_connection();
            }
        }
        
        catch (PDOException $e)
        {
            print "Error!: " . $e->getMessage() . "<br />";
            die();
        }
    }


    static function return_connection()
    {
        return $db_connection;
    }


}


?>

In the above program I keep getting the error -> Error!: SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)     in my web browser but I'm not getting that error at all in /var/log/apache/error.log, just a few undefined variable errors. I can also log into MySQL/MariaDB's root account just fine with a password, but if I try to log in without that password I get the exact error above on my command line.

garrett@mint-desktop /var/www/html/popreport/includes $ mysql -u rootERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
garrett@mint-desktop /var/www/html/popreport/includes $ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 47
Server version: 5.5.46-MariaDB-1ubuntu0.14.04.2 (Ubuntu)


Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MariaDB [(none)]> 

Okay, after looking at those undefined variables in database.php I believe that the file constants.php isn't being included despite the call to include() on line 4 of database.php, any ideas? Thanks.

I'm trying to get the file database to php to return a PDO connection object for other parts of the site so any advice in addition would be much appreciated, thanks again.

 

 

Link to comment
Share on other sites

  • Solution

Admittedly, it's late so I may be overlooking something, but your connection string looks like it should work assuming the username, password, database name, and host are correct. The only thing I see that looks a little off is the space between the host and dbname assignment. The dsn strings that I've seen and used in the past don't have a space there. So perhaps changing

$db_connection = new PDO("mysql:host=$host; dbname=$db_name", $db_user, $password);

to

$db_connection = new PDO("mysql:host=$host;dbname=$db_name", $db_user, $password);

might help? To make sure the constants.php file is being included, just add a die('Hi there! This is constants.php'); at the top of the script an see if it prints out or not. Might be a path issue, although if you have error reporting turned on that should tell you the file isn't found.

 

That having been said, the entire logic behind the class you've designed makes no sense. You're assigning the DB connection to a method-scope variable, then calling a static object method to attempt to return the variable you just assigned to the method-scope variable (and which the static object method knows nothing about) to another method-scope variable?

Edited by maxxd
  • Like 1
Link to comment
Share on other sites

One of two things is true:

1. Your "constants" file is not defining constants. It is defining variables. Variables do not work like constants: variables defined outside a function are not available inside the method.

2. Your constants file is actually defining constants, meaning with define() or const, but you're trying to use variables instead.

 

Because as you can see,

Access denied for user ''@'localhost' (using password: NO)
you are definitely not passing "root" as the user nor providing the password.

 

As for why the error message is not in your logs, that's because it's output. Actual output. From the code. It outputted the message.

print "Error!: " . $e->getMessage() . "
";
Right there.
  • Like 1
Link to comment
Share on other sites

What are you even trying to do, lewashby? What's the purpose of this class?

 

Besides the fact that almost everything about it is technically wrong (as already explained), what's the point of replacing one simple line of code (new PDO...) with 40 lines of code? Even worse: Your class cannot do anything, because it doesn't have any useful methods. All you can do with a Connection instance is somehow grab the underlying PDO connection and throw away the instance.

 

I strongly recommend that you first come up with a sensible concept and then do the implemention. If you just want to pass a bunch of constants to the PDO constructor, you should use a plain function instead of this pseudo-OOP:

<?php

// put those constants into a separate configuration script, if you want
const DB_HOST = 'localhost';
const DB_USER = 'the_username';
const DB_PASSWORD = 'the_password';
const DB_NAME = 'the_db_name';
const DB_CHARSET = 'utf8';



function connectToDatabase()
{
    return new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHARSET, DB_USER, DB_PASSWORD, [
        PDO::ATTR_EMULATE_PREPARES => false,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]);
}
Edited by Jacques1
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.