我有一个构建数据库的功能,如果它们不存在则创建表,但它只适用于一个表。我尝试过使用" /",空间以及&#34 ;;"分离创建表脚本,但没有一个工作。你能帮助我吗? 这是我的功能:
private function buildDB() {
$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS projects(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_czech_ci,
slug VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_czech_ci,
text LONGTEXT CHARACTER SET utf8 COLLATE utf8_czech_ci,
gallery LONGTEXT CHARACTER SET utf8 COLLATE utf8_czech_ci,
date timestamp NOT NULL DEFAULT current_timestamp
);
CREATE TABLE IF NOT EXISTS admins(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_czech_ci DEFAULT 'admin',
password VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_czech_ci DEFAULT '21232f297a57a5a743894a0e4a801fc3'
)
MySQL_QUERY;
return mysql_query($sql);
}
答案 0 :(得分:0)
试试这个,
private function buildDB() {
$sql = "
CREATE TABLE IF NOT EXISTS projects(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_czech_ci,
slug VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_czech_ci,
text LONGTEXT CHARACTER SET utf8 COLLATE utf8_czech_ci,
gallery LONGTEXT CHARACTER SET utf8 COLLATE utf8_czech_ci,
date timestamp NOT NULL DEFAULT current_timestamp
)
";
$sql1 = "
CREATE TABLE IF NOT EXISTS admins(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_czech_ci DEFAULT 'admin',
password VARCHAR(150) CHARACTER SET utf8 COLLATE utf8_czech_ci DEFAULT '21232f297a57a5a743894a0e4a801fc3'
)
";
mysql_query($sql) or die('Invalid query: <br/>'.$sql.' <br/>' . mysql_error());
mysql_query($sql1) or die('Invalid query: <br/>'.$sql1.' <br/>' . mysql_error());
//it would return errors if there are any, else
return "Tables projects and admins created successfully";
}
请注意,不推荐使用mysql函数。改用MySQLi或PDO。从以下查询中获得参考。