0

I have installed PHP, Apache and MySQL manually on my MacBook and I'm following a book about how to create the table without using phpMyAdmin. My script doesn't seem to be creating the DNS, however, it doesn't throw any exceptions. Any suggestions, guys? Thanks in advance.

This is my code: file name: setup.php

<?php
    print("Created.\n");  // This statement prints
    $db = new PDO("mysql:host=localhost;dbname=MyBlog", "username", "password"); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    print("Created.\n"); // This statement does not print
try {
    $queryStr = "CREATE TABLE users (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, 
        name VARCHAR(40), password VARCHAR(100), email VARCHAR(150))";
   $db->query($queryStr);
   print("Created.\n");
} catch (PDOException $e) {
    echo $e->getMessage();
}
Zapato33
  • 53
  • 8

1 Answers1

0

You should write the code where you instatiate and set up the PDO object in the try block. That way you can catch exceptions that occur during connect etc.

vitus37
  • 118
  • 6
  • Thanks for the quick reply. I get this message: SQLSTATE[HY000] [2002] No such file or directory. Can you suggest how I can fix it? – Zapato33 Mar 02 '15 at 18:14
  • This error message has already been answered here multiple times, e.g. [SQLSTATE 2002](http://stackoverflow.com/questions/2412009/starting-with-zend-tutorial-zend-db-adapter-throws-exception-sqlstatehy000). – vitus37 Mar 03 '15 at 06:16