在PHP中删除并创建MySQL视图不起作用

问题描述 投票:0回答:1

我对某些PHP / MySQL代码有疑问。

我正在编写的《星球大战》游戏需要一个名为gameview的视图。

如果我在MySQL中创建视图,则代码可以完美运行。但是,我需要在每次游戏开始时都删除该视图。因此,如果我开始时数据库中没有视图“ gameview”,则由于该视图不存在,因此无法显示该页面。但是,当我手动将视图添加到MySQL时,它可以工作。我不明白为什么。

分类代码

<?php

class gameView
{
    protected $Conn;

    public function __construct($Conn)
    {
        $this->Conn = $Conn;

    }

    public function dropGameView()
    {
        $drop = "DROP VIEW if EXISTS gameview;";
        $stmt = $this->Conn->prepare($drop);
        $stmt->execute(array());
    }
    public function createGameView()
    {
        $view = "CREATE VIEW gameview AS SELECT id, name, image, quote FROM person;";
        $stmt = $this->Conn->prepare($view);
        $stmt->execute(array());
    }

    public function useGameView()
    {
        $query = "SELECT * from gameview";
        $stmt = $this->Conn->prepare($query);
        $stmt->execute(array());
        $gameView = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $gameView;
    }
}

?>

PHP代码

<?php
$gameView = new gameView($Conn);
$finalCharacter = $gameView->useGameView();
$smarty->assign('game_view', $finalCharacter);


?>
php mysql smarty
1个回答
0
投票

嗯。...打乌鸦。我认为这太简单了,但确实做到了!

<?php
$gameView = new gameView($Conn);
$dropGameView = $gameView->dropGameView();
$smarty->assign('drop_gameview', $dropGameView);
$createGameView = $gameView->createGameView();
$smarty->assign('create_gameview', $createGameView);
$finalCharacter = $gameView->useGameView();
$smarty->assign('game_view', $finalCharacter);


?>

现在可以继续使用该视图。

© www.soinside.com 2019 - 2024. All rights reserved.