0

So I'm new to creating a database. I've looked over many other questions similar to mine. I did find something similar with adjusting ports however im not sure how to do that. Nor if it would fix my issue. I am using cleardb if that helps.

<?php
    require_once 'KLogger.php';
    require_once 'dbconfig.php';

    KLogger('/Users/rluth/OneDrive/Documents/GitHub/Website', KLogger::DEBUG);

    try {
        $dsn = "mysql:host = $host;dbname=$db";
        echo "gonna try to connect <br>"; $username . "<br> password " . $password . "<br>";
        $dbn = new PDO($dsn, $username, $password);
        echo "connectoin succesful";
        $dbn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "connectoin succesful";

    }
    catch (Exception $e) {
        echo "connection failed: " . $e->getMessage();
    }
?>

i commented out a lot of things to make sure they werent the issue. Also if someone could let me know what i can use protected for $logger that would be awesome. However, i keep getting this issue.

connection failed: SQLSTATE[HY000] [2002] No such file or directory

i could connect to it by putting in the info directly but i'd rather be able to hide the credentials. I checked to see if the files existed and they do. I checked to see if the credentials were correct and they were. Im unsure of what to do from here. Any help is appreciated

Mahidul Islam
  • 580
  • 11
  • 29
Rutger Luther
  • 131
  • 1
  • 2
  • 11
  • Are you sure your user has permission to the database you are using? I believe (oddly enough) I seen this error for that before for a valid user that didn't have permission to the DB. They were a valid user, just no permission to this DB. – ArtisticPhoenix Mar 30 '18 at 03:50
  • Thanks for the reponse. And ya, like i said i can put in the info straight into it. But its not very safe. So i try and include the login info somewhere else but it doesnt like it that way – Rutger Luther Mar 30 '18 at 03:53
  • I don't understand isn't this putting the connection strait in `$dbn = new PDO($dsn, $username, $password)` ? – ArtisticPhoenix Mar 30 '18 at 03:55
  • Also I think it was where I had `localhost` and the user had privlage to `127.0.0.1` and Windows was using `::1` IPv6. or something, it's been a while. – ArtisticPhoenix Mar 30 '18 at 03:57
  • yes it is which is why i cant figure out whats wrong with it either. Everything prints out how it should be and it works with me putting the info in straight up. And it does use 127.0.0.1 if i try to do it on an addon but i dont like how its setup so im doing it manually. Im just not sure how i would adjust that if it was the issue – Rutger Luther Mar 30 '18 at 04:03
  • You just have to make sure that `host` in the DSN is the same as what the user is set to in the DB. `localhost` may work for either one, it depends it's gotten better over the years. You can also use `%` anyhost in the user permissions, probably not the greatest setting for a production server though.. `mysql:host=127.0.0.1;` – ArtisticPhoenix Mar 30 '18 at 04:27
  • Im using cleardb if that changes anything. And like i said i can get access to it. Thats not the issue. The issue is when i try to put that data in using vars instead of the hardcode – Rutger Luther Mar 30 '18 at 04:33
  • Then obviously the problem is in `'dbconfig.php';` which isn't included. I would make sure your variables are named what you think they are. If it's just a matter of it not working with the Vars, then the vars must be wrong. Can you echo them. – ArtisticPhoenix Mar 30 '18 at 05:44
  • I notice you have this space in here not sure if it's the issue `mysql:host = $host` `\s=\s` There is no diffrence between this `"mysql:host=localhost"` and this `"mysql:host=$host"` as long as `$host='localhost'` – ArtisticPhoenix Mar 30 '18 at 05:45
  • thanks and i did echo them and they came out just how i thoight but ill work on fixing that issue at a later date. I ask my teacher if he can take a look at it. I appreciate the help – Rutger Luther Mar 30 '18 at 17:41
  • Ok, I also tested connection with the space in it as I mentioned you have, and it seems to work fine (on Windows, PHPv5.6) – ArtisticPhoenix Mar 30 '18 at 17:44
  • Thanks but ya ill probably have to have someone who knows what they are doing actually fiddle with it in person since i dont wanna post the credentials on here – Rutger Luther Mar 30 '18 at 19:17

0 Answers0