lewashby Posted December 17, 2015 Share Posted December 17, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/299794-log-in-error-mysqlmariadb/ Share on other sites More sharing options...
Solution maxxd Posted December 17, 2015 Solution Share Posted December 17, 2015 (edited) 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 December 17, 2015 by maxxd 1 Quote Link to comment https://forums.phpfreaks.com/topic/299794-log-in-error-mysqlmariadb/#findComment-1528094 Share on other sites More sharing options...
requinix Posted December 17, 2015 Share Posted December 17, 2015 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/299794-log-in-error-mysqlmariadb/#findComment-1528095 Share on other sites More sharing options...
Jacques1 Posted December 17, 2015 Share Posted December 17, 2015 (edited) 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 December 17, 2015 by Jacques1 1 Quote Link to comment https://forums.phpfreaks.com/topic/299794-log-in-error-mysqlmariadb/#findComment-1528097 Share on other sites More sharing options...
lewashby Posted December 18, 2015 Author Share Posted December 18, 2015 Reconstructed the whole thing, now it works and it's a lot more simple. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/299794-log-in-error-mysqlmariadb/#findComment-1528146 Share on other sites More sharing options...
lewashby Posted December 18, 2015 Author Share Posted December 18, 2015 I don't see a 'Mark Solved' button? Quote Link to comment https://forums.phpfreaks.com/topic/299794-log-in-error-mysqlmariadb/#findComment-1528147 Share on other sites More sharing options...
Ch0cu3r Posted December 18, 2015 Share Posted December 18, 2015 I don't see a 'Mark Solved' button? We have a Best Answer button in the bottom right hand corner of each post. Quote Link to comment https://forums.phpfreaks.com/topic/299794-log-in-error-mysqlmariadb/#findComment-1528158 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.