我正在开发一个 PHP 应用程序,它应该能够使用 SQL 文件列表设置项目数据库并更新它/它的数据定义。这是第一个天真的快速和肮脏的书面程序原型:
<?php
function executeSQLFiles(array $dbOptions, array $dbFiles) {
$dbConnection = mysqli_connect($dbOptions['host'], $dbOptions['user'], $dbOptions['password'], $dbOptions['database']);
if (mysqli_connect_errno($dbConnection)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// db setup
foreach ($dbFiles['setup'] as $listItem) {
$query = file_get_contents(__DIR__ . '/../../config/database/' . $listItem['file']);
$result = mysqli_multi_query($dbConnection, $query);
if (!$result) {
die($listItem['file'] . ': ' . 'Invalid query: ' . mysqli_error($dbConnection) . PHP_EOL);
} else {
echo $listItem['file'] . ' ' . 'OK' . PHP_EOL;
}
}
// db migration
}
它适用于表格,但不适用于视图。我没有收到任何错误,只是没有创建视图,我收到消息“filename.sql OK”。
创建 SQL 脚本的视图(使用 MySQL Workbench 生成)是可以的。当我在 MySQL 客户端中执行它时,会创建一个视图。
-- -----------------------------------------------------
-- Placeholder table for view `allproviders`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `allproviders` (`id` INT, `providertype` INT, `providerid` INT, `displayedname` INT, `url` INT, `city_id` INT);
SHOW WARNINGS;
-- -----------------------------------------------------
-- View `allproviders`
-- -----------------------------------------------------
DROP VIEW IF EXISTS `allproviders` ;
SHOW WARNINGS;
DROP TABLE IF EXISTS `allproviders`;
SHOW WARNINGS;
DELIMITER $$
CREATE OR REPLACE VIEW `allproviders` AS
SELECT
`providers`.`id`,
`providers`.`type` AS `providertype`,
`providers`.`providerid` AS `providerid`,
`universities`.`displayedname`,
`universities`.`url`,
`universities`.`city_id`
FROM
`providers`
JOIN
`universities` ON `providers`.`providerid` = `universities`.`id`
UNION
SELECT
`providers`.`id`,
`providers`.`type` AS `providertype`,
`providers`.`providerid` AS `providerid`,
`partners`.`displayedname`,
NULL `url`,
`partners`.`city_id`
FROM
`providers`
JOIN
`partners` ON `providers`.`providerid` = `partners`.`id`
$$
DELIMITER ;
;
SHOW WARNINGS;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
难道我做错了什么?如何让它工作?
谢谢