我用下面的要點作出OOP嘗試創建數據庫連接:PHP數據庫表的創建失敗
https://gist.github.com/jonashansen229/4534794
它似乎工作至今。
但是,創建數據庫表的時候會失敗。
編輯:
最近的意見和建議後,我更新了我的代碼:
require_once 'Database.php'; // the gist 4534794
class DatabaseSchema {
public function createStudents() {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$create_students = 'CREATE TABLE IF NOT EXISTS students (
id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(40) NOT NULL,
lastname VARCHAR(40) NOT NULL,
university VARCHAR(50)
)';
$result = $mysqli->query($create_students);
}
public function createPassedExams() {
$db = Database::getInstance();
$mysqli = $db->getConnection();
$create_passed_exams = 'CREATE TABLE IF NOT EXISTS passed_exams (
id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(40) NOT NULL,
student_id INT(6),
FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE
)';
$result = $mysqli->query($create_passed_exams);
}
}
$db_student = new DatabaseSchema();
$db_student->createStudents();
$db_student->createPassedExams();
當我在mysql控制檯的外觀,僅創建表的學生。 表爲什麼表失傳?
$ db_student = new DatabaseSchema()最後缺少';'。 –
哦,男孩,謝謝@Arsh,完全被忽視。 – StandardNerd
不客氣。 –