所以我正在设置这样的连接:
<?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/
应该有一些显示的数据,当我尝试添加任何内容时,它会给我下一个错误:
GET 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";
}
?>
我已经检查了所有内容,它在我的本地主机上运行良好,一切正常,但如果我试图连接到 Heroku 上托管的 ClearDB,它就不起作用。
ps我在composer.json中有这个:
{
"require": {
"ext-mysql": "*"
}
}
pss 这是我的 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();
}
};
?>
psss 我发现了这个 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 服务器上的数据库是空的?