1

I have been using this same exact code and now with this new project, I can't get it to connect to my database. It is running on the same server and everything as the older ones.

In fact, I even copied and pasted this info into my new project from an old project, replaced the password and username fields to match this project, and It did not work.

The code is:

<?php

    $host = 'localhost';

    $dbname = '';

    $user = '';

    $pass = '';

    $db = new PDO('mysql:host='.$host.';dbname='.$dbname.'', $user, $pass);

?>

I get this error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory'

What difference would it be making? I don't understand. it's in the same root as the other projects just in a diff. folder.

--root
   -- project 1
   -- project 2 
Mitch Evans
  • 103
  • 1
  • 4
  • 9
  • Base yourself on this example `$db = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);` I think your quotes may be off. – Funk Forty Niner Feb 24 '14 at 21:32
  • If what @Fred-ii- says doesn't work try this: http://stackoverflow.com/a/2412064/1086938 – hanleyhansen Feb 24 '14 at 21:33
  • First of all, I suggest using 127.0.0.1 as the host. Using PDO it has been proven to be more efficient than localhost. Secondly, please don't leave the $dbname empty and supply a dbname. Moreover, if possible, please remove the `.''` at the end of $dbname, it seems to be unrelated and may cause a problem. – kfirba Feb 24 '14 at 21:35
  • Same result with both.... – Mitch Evans Feb 24 '14 at 21:36
  • @kfirba - Clearly, the parameters that are empty above where posted "empty" for a reason. In the actual code they are filled. Not providing the world with my database connection credentials! – Mitch Evans Feb 24 '14 at 21:38
  • Are you using any escape characters (backslash) for your password by any chance? @MitchEvans – Funk Forty Niner Feb 24 '14 at 21:40
  • @MitchEvans Considering the example, I thought it was left blank. Next time you better use even fake credentials. No one asked for real ones. Is this happening with this project only? Have you used this code in any other project and it worked? – kfirba Feb 24 '14 at 21:40
  • Yes it works with every other project as stated in the question. Just not this one. I've even tried connecting to a different database with this same file and it gives me that error. If I change `localhost` to `127.0.0.1` it gives me `connection refused` – Mitch Evans Feb 24 '14 at 21:43
  • Is this happening for your DB connection only, or are you querying also? – Funk Forty Niner Feb 24 '14 at 21:44
  • Nothing works without the connection... – Mitch Evans Feb 24 '14 at 21:47
  • I'm personally baffled at this point, as I'm sure you are also. I upvoted your question, hoping someone else can shed some light on the subject. @MitchEvans – Funk Forty Niner Feb 24 '14 at 21:50
  • I just don't understand how I can copy and past from one project to the next and have the next project not work when the last project works just fine... – Mitch Evans Feb 24 '14 at 21:51
  • Then make a copy of the exact file and change only 1 or 2 things, see what happens then. @MitchEvans - It could be a quote, a dot... etc. could be the smallest thing. – Funk Forty Niner Feb 24 '14 at 21:53
  • I did. Copied and pasted. – Mitch Evans Feb 24 '14 at 21:54
  • You're missing the port from your `127.0.0.1`. It might make a difference, use this as your connection string: `mysql:host=127.0.0.1:3306;dbname=your_database_name` – Phil Cross Feb 24 '14 at 22:54

1 Answers1

0

The error is probably in database name. You can create a PDO without using database name. The code below lists the databases.

try{
$dsn = "mysql:host=localhost";
$dbh = new PDO($dsn, $user, $pass);
$stmt ="SHOW DATABASES";
foreach($dbh->query($stmt) as $row){
    echo $row['Database'];
    echo"<br>";
    }
}  
catch(PDOException $e) {  
    echo  $e->getMessage() ;
}  

PS check for leading spaces in names

david strachan
  • 7,174
  • 2
  • 23
  • 33