MySQLi行未加载

时间:2013-09-01 20:07:58

标签: php html mysql mysqli rows

我有一个基本表单,在每个框中加载15个相同主题的下拉框。这是一个投票页面,用户可以投票选出他最喜欢的主题或他最不喜欢的主题。我遇到的问题是,当我告诉他们时,主题并没有被加载。这是我的代码。

PHP

<?php
$Vote = new Vote();

class Vote {

    public function GetTopic() {
        $Connect = new mysqli("127.0.0.1", "root", "", "Data");

        $Query = 'SELECT * FROM Topics';

        if($Gather = $Connect->query($Query))
        {
            while($Row = $Gather->fetch_assoc())
            {
                $Topic = $Row['Topic'];
                echo '<option>'.$Topic.'</option>';
            }

            $Gather->free();
        }
        else
        {
            echo 'Error';
        }

        $Connect->close();
    }

    public function LoadTopic() {
        for($I = 15; $I > 0; $I--)
        {
            echo '<select><option>'.$I.'</option>'.$this->GetTopic().'</select>';
        }
    }

}
?>

2 个答案:

答案 0 :(得分:0)

如果你使用这样的函数,你应该返回你的html数据而不是输出它:

public function GetTopic() {
    $Connect = new mysqli("127.0.0.1", "root", "", "Data");

    $Query = 'SELECT * FROM Topics';

    if($Gather = $Connect->query($Query))
    {
        $html = "";
        while($Row = $Gather->fetch_assoc())
        {
            $Topic = $Row['Topic'];
            $html .= '<option>'.$Topic.'</option>';
        }
        $Gather->free();
        return $html;
    } else
    {
        //handle error
    }
    $Connect->close();
}

答案 1 :(得分:0)

让我们尝试一些更合适的课程:

<?php

    class Vote
    {
        private $connect;

        public $topics = array();

        public function __construct()
        {
            $this->connect = new mysqli( '127.0.0.1', 'root', '', 'Data' );

            if( $this->connect->connect_errno )
            {
                echo "Error:(" . $this->connect->connect_errno . "): " . $this->connect->connect_error . ".";
            }
        }

        public function GetTopics()
        {
            $Query = 'SELECT * FROM Topics';

            if( $Gather = $this->connect->query( $Query ) )
            {
                while( $Row = $Gather->fetch_assoc() )
                {
                    $this->topics[] = $Row['Topic'];
                }
                $Gather->free();
            }
            else
            {
                echo 'Error';
            }
        }

        public function LoadTopics()
        {
            if( $max = count($this->topics) > 0 )
            {
                $html = "<select>\r\n";

                for( $i = 0; $i < $max; ++$i )
                {
                    $html .= "<option value=" . $i . ">" . $this->topics[$i] . "</option>";
                }
                $html .= "</select>\r\n";

                return $html;
            }
            else
            {
                return false;
            }
        }

        public function __destruct()
        {
            $this->connect->close();
        }
    }
?>

__construct()/ __destruct()方法实际上是为了建立你的连接。你也可以组合两个函数,只需要GetTopics()方法(我强制改变了一些方法和属性名)运行查询,格式化结果并返回$html

此外,我升级了您的for函数,如果您决定稍后在主题中添加其他条目,它将随之展开,而不是通过15个静态行计数。

您可以使用以下方式调用它:

<?php

    $vote = new Vote();

    echo $vote->GetTopics()->LoadTopics();
?>

我看到答案已被选中,不希望我的工作浪费; D

备用GetTopics()功能,全部合并为一个。

        public function GetTopics()
        {
            $Query = 'SELECT * FROM Topics';

            if( $Gather = $this->connect->query( $Query ) )
            {
                $html = "<select>\r\n";
                $i = 0;

                while( $Row = $Gather->fetch_assoc() )
                {
                    $html .= "<option value=" . $i . ">" . $Row['Topic'] . "</option>";
                    ++i;
                }
                $html .= "</select>\r\n";

                $Gather->free();

                return $html;
            }
            else
            {
                return "Error: No Results Returned";
            }
        }

现在它刚刚被称为:

<?php echo $vote->GetTopics(); ?>