【问题标题】:MySQLi Rows Not LoadingMySQLi 行未加载
【发布时间】:2013-09-01 20:07:57
【问题描述】:

我有一个基本表单,它可以加载 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>';
        }
    }

}
?>

【问题讨论】:

  • 你做过任何调试吗?您的查询有效吗?甚至调用 GetTopic 吗?数据库连接是否有效?
  • 在上面添加你的 php 代码,用于调试:error_reporting(E_ALL ^ E_NOTICE);ini_set("display_errors", 1);
  • 是的,我的查询正在运行,我已经通过调用我页面上的函数来回应它们。正在加载所有主题。我正在尝试将它们加载到选项标签中,但它不起作用。
  • 我在这里看到的最大问题是,除了将 $vote 声明为 Vote 类之外,您没有对它做任何事情。
  • @Jason 我在我的 HTML 中调用 vote 变量来加载所有内容。加载选择标签没有问题,因为我在我的页面上生成了 15 个标签

标签: php html mysql mysqli rows


【解决方案1】:

如果你像这样使用你的函数,你应该返回你的 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();
}

【讨论】:

  • 这行得通!我有一个问题,当我有 15 个时它只返回一行
  • 我修好了!我不得不将 return $Html 移到我的 while 循环之外
  • 尝试新的数据库连接每个select
【解决方案2】:

让我们尝试一些更适合类的东西:

<?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(); ?>

【讨论】:

    猜你喜欢
    • 2021-08-13
    • 2015-04-09
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2017-04-07
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多