0

So I have a page, let's call it page1.php that is requiring another page (page2.php) which also requires another page (page3.php). Now page3.php just contains four variables which are the database server's address, username, password, and database to select.

Page2.php simply contains functions that interact with the server based on page3.php's variables. Here is a sample of that code.

$conn = new PDO("mysql:host=$pdo_conn;dbname=$pdo_database", $pdo_username, $pdo_password);

Whenever I run the script, this fatal error occurs...

Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user ''@'10.24.3.40'

So the hostname is inserted but the username and password isn't. When I manually change the variables to the actual string containing the username and password, it works completely fine. Why is this happening?

Carl Flood
  • 37
  • 3
  • This link may help: https://stackoverflow.com/questions/31154124/sqlstatehy000-1045-access-denied-for-user-usernamelocalhost-using-cakep – Mahbubul Islam Aug 28 '17 at 03:38
  • @MahbubulIslam nope that doesn't help. I am absolutely positive that the credentials are correct. The question clearly states that when I put the credentials directly in rather than using variables, the connection succeeds properly. – Carl Flood Aug 28 '17 at 03:44
  • Show us your page3. – Mahbubul Islam Aug 28 '17 at 03:48
  • What you are not seeing is the error message *"Notice: Undefined variable: pdo_username"* among others because your error reporting level is too low. See [this answer](https://stackoverflow.com/a/45854160/283366) for instructions on how to increase your error reporting levels. – Phil Aug 28 '17 at 04:14

1 Answers1

0

Are you including the other PHP file before that connection code?

Insert the following above your new PDO line and let us know if it outputs the vars correctly:

var_dump("username: " . $pdo_username, "password: " . $pdo_password);
flauntster
  • 2,008
  • 13
  • 20
  • That works when placed outside of the function running the PDO connection, however when placed inside of the function, it doesn't work. I don't think it's a scope issue though because the hostname is being inserted fine. What do you think? – Carl Flood Aug 28 '17 at 03:42
  • Interesting that the hostname var is working within the function, could you post up the function you're using? – flauntster Aug 28 '17 at 03:47