php数据库表创建失败
我使用以下要点进行OOP尝试以创建数据库连接: 到目前为止,它似乎奏效了 但是创建数据库表失败 编辑: 在最近的评论和建议之后,我更新了我的代码:php数据库表创建失败,php,mysqli,Php,Mysqli,我使用以下要点进行OOP尝试以创建数据库连接: 到目前为止,它似乎奏效了 但是创建数据库表失败 编辑: 在最近的评论和建议之后,我更新了我的代码: require_once 'Database.php'; // the gist 4534794 class DatabaseSchema { public function createStudents() { $db = Database::getInstance(); $mysqli = $db->getConn
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控制台时,只创建表students。
为什么缺少表passed_考试?您创建字符串来检查
$query_students='SELECT ID FROM students'代码>
但你从来没有真正运行过这个。然后检查字符串是否为空,它在代码中永远不会为空。
你应该做的是使用创建。。。如果不存在mysql的语法,那么这里就没有您要做的
第一个示例显示语法您的学生表上的id
列是INT(6)UNSIGNED
,但是通过考试表上的学生id
列是有符号的INT(6)
。因此,外键(student_id)引用DELETE CASCADE上的student(id)
子句将失败,并显示“错误代码:1215.无法添加外键约束”
我建议您实现一些错误处理,以便看到此错误消息,而不是盲目地继续执行代码。$db_student=new DatabaseSchema()有代码>结尾处缺失。哦,孩子,谢谢@Arsh,完全忽略了。这不是OOP,所以使用DatabaseSchema::CreateStustudents()@唉,问题仍然存在。@StandardNerd现在什么是错误?完全同意你的看法。由于字符串有一个文本,所以它永远不会为空,他正在检查它是否为空,如果为空,则运行查询。。。如果不存在
,我只是更新了我的代码。现在将创建表student,但仍然缺少通过考试的表。对于上一个问题-创建通过考试的表-Matt提供的答案是解决方案。但是,有没有办法为你给出的答案给你一些分数呢?我想,我应该结束这个问题,打开一个新的问题。对不起Malimovka,下次我会的。