3

I have been trying to connect to my database through PHP for a few hours now, with no success. My PHP code looks like this:

<?php

    ini_set('display_errors', 'On');

    $hostname = 'localhost';
    $username = 'root';
    $password = '';

    function testdb_connect($hostname, $username, $password) {
        $db = new PDO("mysql:host=$hostname;dbname=asdf", $username, $password);
        return $db;
    }

    try {
        $db = testdb_connect($hostname, $username, $password);
        echo 'Connected to database';
    } catch(PDOException $e) {
        echo $e->getMessage();
        }

    ?>

What I have already tried:

  • Checked if 'root' is configured to 'localhost' (Y)
  • Checked if root@localhost has Grant_priv/Super_priv permissions (Y for both)
  • Checked if Apache/MySQL is running (Y for both) Checked if my PHP
  • Code is wrong, i.e. I am missing something (N)

Any ideas where the Problem could lie? Thanks in advance!

EDIT:

I am so sorry, I have seen the error message so often today, I forgot to mention it here:

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)

RE-EDIT:

Here the php code with the user I created:

<?php

    ini_set('display_errors', 'On');

    $hostname = 'localhost';
    $username = 'myUserName';
    $password = 'thatUserPassword';

    function testdb_connect($hostname, $username, $password) {
        $db = new PDO("mysql:host=$hostname;dbname=asdf", $username, $password);
        return $db;
    }

    try {
        $db = testdb_connect($hostname, $username,    $password);
        echo 'Connected to database';
    } catch(PDOException $e) {
        echo $e->getMessage();
    }

    ?>

Error Message:

SQLSTATE[HY000] [1045] Access denied for user 'myUserName'@'localhost' (using password: YES)
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137

2 Answers2

3

Let's say I do this:

A

<?php
    $db = new PDO('mysql:host=localhostaaa;dbname=so_gibberish;charset=utf8', 'myUser', 'myPassword');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    echo "I am right here<br/>";
?>

Then upon errors, I get a white screen.

Let's say I do this:

B

<?php
    try {
        $db = new PDO('mysql:host=localhostxxxx;dbname=so_gibberish;charset=utf8', 'fred', 'password');
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
        exit();
    }
    echo "You are connected<br/>";
?>

Then B gives me connect error information

Edit:

Based on that edit to Question, please have a user created with a password, and pass that password, not a blank string.

It is important to not connect as root, as it is an admin account for maintenance. Using root exposes you to the risk of having scripts comprised and credentials stolen (if so baked-in and not in a vault), thus allowing a hacker to gain access to all your databases.

Error:

[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)

Could be issues with my.cnf file and bind-address setting. Need to see that.

What I would suggest, is create a non-root user 'jimmy987'@'%', and do a

SET PASSWORD FOR 'jimmy987'@'%'=PASSWORD('');

See if it works with that user, then drop the user. Note that the error message will change if successful, perhaps barking about no rights to use the database. Because the Grants were not performed.

Based on articles that seems to suggest the server is expecting a blank password.

Then we need to fix why.

Outside of that, I would have to do a TeamViewer session to see what is up (as I certainly don't see what you are really running). I would love to know the answer when you fix it.

Drew
  • 24,851
  • 10
  • 43
  • 78
2

Since you did not mention the exact errors, I'm afraid it'll be hard to guess for most of us. Try this for a change?

<?php

$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "asdf";

try
{
 $db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
 $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
 $e->getMessage();
}
?>
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • 2
    who knows, we can't buy cheeseburgers with these points anyway. Just help and move on :) – Drew Sep 13 '15 at 19:31
  • I added the error message in the question now. Any ideas how to fix it? – MoneyIsAMotivation Sep 13 '15 at 20:32
  • @MoneyIsAMotivation, you need to have/create a user with a password and then give the credentials instead of empty password string. or you could just change the password for root and give that password instead. read more here: http://stackoverflow.com/questions/2995054/access-denied-for-user-rootlocalhost-using-passwordno – DirtyBit Sep 13 '15 at 20:48
  • @HawasKaPujaari already tried that and it didn't work... (that was my initial attempt) – MoneyIsAMotivation Sep 13 '15 at 20:53