我正在研究一个PHP安装脚本,它将在MySQL中创建所需的数据库,表和视图。当我在Internet Explorer中运行脚本时,它将返回黑屏(如果没有错误,则按预期进行)。
当我在phpMyAdmin中查看数据库时,我看到了所有表,但是视图不存在。当我通过phpMyAdmin的SQL窗口运行相同的CREATE VIEW
SQL语句时,该视图将按预期方式创建。
我在SQL方面有丰富的经验,但是我是PHP和phpMyAdmin的新手。为什么在表创建时视图不会从PHP脚本创建?
PHP脚本:
<html>
<head>
<title>Install Flashcard Script</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'>
<link href='custom.css' rel='stylesheet' type='text/css'>
</head>
<body>
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "FLASHCARD";
$mysqli = new mysqli($host, $user, $password) or die(mysqli_error($mysqli));
$mysqli->query("DROP DATABASE IF EXISTS " . $database) or die($mysqli->error);
$mysqli->query("CREATE DATABASE " . $database) or die($mysqli->error);
$mysqli = new mysqli($host, $user, $password, $database) or die(mysqli_error($mysqli));
$mysqli->query("CREATE TABLE COURSES (
COURSE_ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
COURSE_NAME VARCHAR(255) NOT NULL,
COURSE_DESC TEXT NOT NULL,
DATE_CREATED DATETIME NOT NULL,
CREATED_BY BIGINT(20)
)") or die($mysqli->error);
$mysqli->query("CREATE TABLE COURSE_SUBJECTS (
SUBJ_ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
COURSE_ID INT(6),
SUBJ_NAME VARCHAR(255) NOT NULL,
SUBJ_DESC TEXT,
DATE_CREATED DATETIME NOT NULL,
CREATED_BY BIGINT(20)
)") or die($mysqli->error);
$mysqli->query("CREATE TABLE SUBJECT_DECKS (
DECK_ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
SUBJ_ID INT(6) NOT NULL,
DECK_NAME VARCHAR(255) NOT NULL,
DECK_DESC TEXT,
DATE_CREATED DATETIME NOT NULL,
CREATED_BY BIGINT(20)
)") or die($mysqli->error);
$mysqli->query("CREATE VIEW DECK_VIEW AS
SELECT
c.COURSE_ID,
cs.SUBJ_ID,
sd.DECK_ID,
c.COURSE_NAME,
c.COURSE_DESC,
cs.SUBJ_NAME,
cs.SUBJ_DESC,
sd.DECK_NAME,
sd.DECK_DESC
FROM COURSES c
LEFT JOIN COURSE_SUBJECTS cs ON cs.COURSE_ID = c.COURSE_ID
LEFT JOIN SUBJECT_DECKS sd on sd.SUBJ_ID = cs.SUBJ_ID
ORDER BY COURSE_NAME, SUBJ_NAME, DECK_NAME
)") or die($mysqli->error);
?>
</body>
</html>
**编辑
我在CREATE VIEW
上缺少错误捕获语句。现在,我在其中添加了它,它给了我以下错误消息:
您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以在第16行的')'附近使用正确的语法
第16行对应于$database = "FLASHCARD";
。是什么意思?
答案 0 :(得分:2)
第16行不是您想的那样,而是在查询本身中。
您在上一个查询中错过了方括号,并且在该查询中也应该使用mysqli_error($mysqli)
。
因此将您的代码更改为
CREATE VIEW DECK_VIEW AS ( SELECT...
^ // right there