Php 坚持尝试从mysql数据库中显示tictactoe值

Php 坚持尝试从mysql数据库中显示tictactoe值,php,mysql,Php,Mysql,我正在用php和mysql制作一个tictactoe游戏。它应该由来自不同系统的两名玩家播放。所以我在mysql中有一个表,其列id为1-9,列tictac的值为X、O或NULL。在数据库中放置x或o正在工作,但从表中检索x或o并在屏幕上显示它不工作。我有一个配置文件,一个class.tictactoe.php、class.game.php和一个index.php。DB连接应该在class.tictactoe.php中,但我不知道如何连接 这部分我遇到了最大的麻烦: function displ

我正在用php和mysql制作一个tictactoe游戏。它应该由来自不同系统的两名玩家播放。所以我在mysql中有一个表,其列id为1-9,列tictac的值为X、O或NULL。在数据库中放置x或o正在工作,但从表中检索x或o并在屏幕上显示它不工作。我有一个配置文件,一个class.tictactoe.php、class.game.php和一个index.php。DB连接应该在class.tictactoe.php中,但我不知道如何连接

这部分我遇到了最大的麻烦:

function displayGame()
{

    if (!$this->isOver())
    {
        echo "<div id=\"board\">";
        $i=1;
        for ($x = 0; $x < 3; $x++)
        {
            for ($y = 0; $y < 3; $y++)
            {
                require('config.inc.php');

                $db = new PDO("mysql:dbname=$db_name;host=$db_host",
                    $db_user, $db_pass,
                    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);


                echo "<div id=\"$i\" class=\"board_cell\">";
                $i++;
                $query = $db->prepare('SELECT * FROM tictactoe WHERE value IS NOT NULL and id = ?');
                if ($this->board[$x][$y])
                    $query->execute([$lookup[$key]]);
                echo "<p>\"{$this->board[$x][$y]}\"</p>";
            else
                {
                    echo "<select name=\"{$x}_{$y}\">
                                <option value=\"\"></option>
                                <option value=\"{$this->player}\">{$this->player}</option>
                            </select>";
                }

                echo "</div>";
            }

            echo "<div class=\"break\"></div>";
        }

        echo "
                <p align=\"center\">
                           <input type=\"submit\" name=\"move\" value=\"Take Turn\" /><br/>
                    <b>It's player {$this->player}'s turn.</b></p>
            </div>";
    }
    else
    {

        if ($this->isOver() != "Tie")
            echo successMsg("Congratulations player " . $this->isOver() . ", you've won the game!");
        else if ($this->isOver() == "Tie")
            echo errorMsg("Whoops! Looks like you've had a tie game. Want to try again?");

        session_destroy();

        echo "<p align=\"center\"><input type=\"submit\" name=\"newgame\" value=\"New Game\" /></p>";
    }
}

function move($postdata)
{

    if ($this->isOver())
        return;
    require('config.inc.php');

    $db = new PDO("mysql:dbname=$db_name;host=$db_host",
        $db_user, $db_pass,
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

    $postdata = array_unique($postdata);
    $lookup = ['0_0'=>1, '0_1'=>2, '0_2'=>3, '1_0'=>4, '1_1'=>5, '1_2'=>6,
               '2_0'=>7, '2_1'=>8, '2_2'=>9];
    $query = $db->prepare('UPDATE tictactoe SET tictac = ? WHERE id = ?');

    foreach ($postdata as $key => $value)
    {
        if ($value!=$this->player) continue;
        $query->execute([$this->player, $lookup[$key]]);
        if ($value == $this->player)
        {
            $coords = explode("_", $key);
            $this->board[$coords[0]][$coords[1]] = $this->player;

            if ($this->player == "X")
                $this->player = "O";
            else
                $this->player = "X";

            $this->totalMoves++;
        }
    }

    if ($this->isOver())
        return;
}
函数displayGame()
{
如果(!$this->isOver())
{
回声“;
$i=1;
对于($x=0;$x<3;$x++)
{
对于($y=0;$y<3;$y++)
{
要求('config.inc.php');
$db=newPDO(“mysql:dbname=$db\u name;host=$db\u host”,
$db_用户,$db_通行证,
[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
回声“;
$i++;
$query=$db->prepare('SELECT*fromtictactoe,其中值不为NULL,id=?');
如果($this->board[$x][$y])
$query->execute([$lookup[$key]]);
回声“\”{$this->board[$x][$y]}\”

”; 其他的 { 回声“ {$this->player} "; } 回声“; } 回声“; } 回声“


轮到玩家{$this->player}了

"; } 其他的 { 如果($this->isOver()!=“Tie”) echo Successsg(“恭喜玩家”。$this->isOver()。”,你赢了游戏!); 否则如果($this->isOver()=“Tie”) echo errorMsg(“哎哟!看起来你打了一场平局。想再试一次吗?”); 会话_destroy(); 回声“

”; } } 函数移动($postdata) { 如果($this->isOver()) 返回; 要求('config.inc.php'); $db=newPDO(“mysql:dbname=$db\u name;host=$db\u host”, $db_用户,$db_通行证, [PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]); $postdata=array_unique($postdata); $lookup=['0_0'=>1,'0_1'=>2,'0_2'=>3,'1_0'=>4,'1_1'=>5,'1_2'=>6, '2_0'=>7, '2_1'=>8, '2_2'=>9]; $query=$db->prepare('UPDATE tictactoe SET tictac=?WHERE id=?'); foreach($postdataas$key=>$value) { 如果($value!=$this->player)继续; $query->execute([$this->player,$lookup[$key]]); 如果($value==$this->player) { $coords=explode(“\”,$key); $this->board[$coords[0][$coords[1]]=$this->player; 如果($this->player==“X”) $this->player=“O”; 其他的 $this->player=“X”; $this->totalMoves++; } } 如果($this->isOver()) 返回; }


压痕可能是错误的

使用
值不为空
而不是
=
我想这应该是一个类的一部分,这个类包含
$this
变量。我建议不要每次都加载配置文件,而是在
\uu construct()
函数中加载它,然后可以将db连接保存到类变量。