0

I have tried everything but i keep getting this error.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host

Is it possible that my server is blocking it? How do i fix it

$dbh = new PDO('mysql:host=$host;dbname=$database',$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

and the dbinfo.php is:

$host="mysql.spri.co.za;port=3307"; $database="kruger_park_live"; $username="*****"; $password="*****";
Sean Konig
  • 27
  • 1
  • 12

1 Answers1

0

Use this:

$dbh = new PDO("mysql:host=$host;dbname=$database",$username,$password);

When you use single quotes variables are not being replaced with their values.

k102
  • 7,861
  • 7
  • 49
  • 69
  • I have tried this, it is not working, maybe there is a connection problem – Sean Konig May 27 '13 at 10:40
  • So can you connect to the server using some tool (like Mysql Workbench) with this credentials? – k102 May 27 '13 at 10:42
  • well it changed the error code: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server – Sean Konig May 27 '13 at 10:48
  • yep I am logged into the server and I just pinged it to see what happens – Sean Konig May 27 '13 at 10:50
  • @SeanKonig try error handling, like `try{}catch(){}` it will echo out, a precise error, as to why you are getting the error – samayo May 27 '13 at 11:12
  • @php NoOb sorry this is the first time i am really using php to do something like this and I dont know where to add what you are suggesting – Sean Konig May 27 '13 at 11:13
  • @SeanKonig check this link. http://pastebin.com/mcb0vT5V and run your code, it will tell you where the problem is – samayo May 27 '13 at 11:20
  • Call to a member function setAttribute() on a non-object in /home/sea503/public_html/phpsqlinfo_addrow.php on line 20
    – Sean Konig May 27 '13 at 11:30
  • this my code: try { $dbh = new PDO('mysql:host=$host;dbname=$database',$username,$password); }catch(PDOException $e){ echo 'ERROR:'.$e->getMessage(); } $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { – Sean Konig May 27 '13 at 11:32
  • @phpNoOb this whole thing is no totally confusing me – Sean Konig May 27 '13 at 11:49
  • Remove the setAttribute for now, and try your query again. – samayo May 27 '13 at 11:51
  • now i get this: Data Loaded: ERROR:SQLSTATE[HY000] [2005] Unknown MySQL server host '$host' (25)
    Fatal error: Call to a member function prepare() on a non-object in /home/sea503/public_html/phpsqlinfo_addrow.php on line 20
    – Sean Konig May 27 '13 at 12:11