无法与Heroku上托管的ClearDB进行交互

时间:2015-05-05 16:17:11

标签: php mysql heroku cleardb

所以我设置了这样的连接:

<?php
define("DB_HOST", 'us-cdbr-iron-east-02.cleardb.net');
define("DB_USER", 'bc2****06bc220');
define("DB_PASS", '04****ae08');
define("DB_NAME", 'heroku_7da****a5ce8ba');   
?>

然后我做了一个我的数据库的转储(localhost.sql),我正在执行它:

"mysql --host=us-cdbr-iron-east-02.cleardb.net --user=bc2dc****6bc220 --reconnect heroku_7****4ca5ce8ba < localhost.sql"

此后不会出现任何错误。

但是没有别的: http://ajaxaddressbook.herokuapp.com/

应该有一些显示的数据,当我试图添加任何东西时,它会给我下一个错误:

  

获取http://ajaxaddressbook.herokuapp.com/contacts.php 500(内部服务器错误)

     

POST http://ajaxaddressbook.herokuapp.com/add_contact.php 500(内部服务器错误)

这就是我试图添加联系人的方式(我正在使用PDO):

<?php include 'core/init.php'; ?>
<?php 
//Create DB Object
$db = new Database;

//Run Query
$db->query("INSERT INTO `addressbook_contacts` (first_name, last_name, email, phone, address1, address2, city, state,  zipcode, contact_group, notes)
            VALUES (:first_name, :last_name, :email, :phone, :address1, :address2, :city, :state, :zipcode, :contact_group, :notes)");

//Bind Values
if($_POST){
    $db->bind(':first_name', $_POST['first_name']);
    $db->bind(':last_name', $_POST['last_name']);
    $db->bind(':email', $_POST['email']);
    $db->bind(':phone', $_POST['phone']);
    $db->bind(':address1', $_POST['address1']);
    $db->bind(':address2', $_POST['address2']);
    $db->bind(':city', $_POST['city']);
    $db->bind(':state', $_POST['state']);
    $db->bind(':zipcode', $_POST['zipcode']);
    $db->bind(':contact_group', $_POST['contact_group']);
    $db->bind(':notes', $_POST['notes']);

    if($db->execute()){
        echo "Contact was added";
    } else{
        echo "Could not add contact";
    }
} else{
    echo "Could not add contact";
}

?>

我已经检查了所有内容,并且它在我的localhost上工作正常,但是如果我想连接到Heroku上托管的ClearDB则无法正常工作。

P.S。我在composer.json中有这个:

{
    "require": {
        "ext-mysql": "*"
    }
}

p.s.s。这是我的PDO课程:

<?php
    class Database{
        private $host   = DB_HOST;
        private $user   = DB_USER;
        private $pass   = DB_PASS;
        private $dbname = DB_NAME;

        private $dbh;
        private $error;
        private $stmt;   

        public function __construct(){
            // Set DSN
            /*
             * A data source name (DSN) is a data structure that contains the information about a specific database that an Open Database Connectivity ( ODBC ) driver needs in order to connect to it. 
             */
            $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
            // Set Options
            $options = array(
                PDO::ATTR_PERSISTENT => true,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
            );
            // Create a new PDO instance
            try{
                $this -> dbh = new PDO($dsn, $this->user, $this->pass, $options);
            }   // Catch any Errors
            catch(PDOException $e){
                $this->error = $e->getMessage();
            };
        }

        public function query($query){
            $this->stmt = $this->dbh->prepare($query);
        }

        public function bind($param, $value, $type = null){
            if(is_null ( $type )){
                switch(true){
                    case is_int ($value):
                        $type = PDO::PARAM_INT;
                    case is_bool ($value):
                        $type = PDO::PARAM_BOOL;
                    case is_null ($value):
                        $type = PDO::PARAM_NULL;
                    break;
                    default:
                        $type = PDO::PARAM_STR;
                }
            }
            $this->stmt->bindValue ($param, $value, $type);
        }

        public function execute(){
            return $this->stmt->execute();
        }

        public function resultset(){
            $this->execute();
            return $this->stmt->fetchAll(PDO::FETCH_OBJ);
        }

        public function single(){
            $this -> execute();
            return $this->stmt-> fetch(PDO::FETCH_OBJ);
        }

        public function rowCount(){
            return $this->stmt->rowCount();
        }

        public function lastInsertId(){
            return $this->dbh->lastInsertId();
        }

        };
?>

p.s.s.s。我发现了这个PDO例外:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'heroku_7da7af4ca5ce8ba.addressbook_contacts' doesn't exist' in /app/libraries/Database.php:54   

看起来我的转储没有执行,Heroku服务器上的数据库是空的?

0 个答案:

没有答案