如何创建一个mysqli_multi_query看法?(How to create a view w

2019-10-17 19:47发布

我开发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工作台产生的)就可以了。 当我在一个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;

难道我做错了什么? 如何得到它的工作?

谢谢

文章来源: How to create a view with mysqli_multi_query?