【问题标题】:Making SQL query from php and showing to HTML从 php 进行 SQL 查询并显示为 HTML
【发布时间】:2015-01-20 10:47:05
【问题描述】:

我定制了这个页面mysql.php,但目前它在浏览器上显示为空白。

<html>
    <head>
        <title> MySQL Test </title>
    </head>
    <body>

        <?php
            $username = "username";
            $password = "pass";
            $hostname = "localhost";
            $dbname = "StudentLessons";

            //connection to the database
            $dbhandle = mysql_connect($hostname, $username, $password, $dbname)
              or die("Unable to connect to MySQL");
            echo "Connected to MySQL <br>";

            $query = "SELECT * FROM Teacher GROUP BY bathmida";
            $result = mysql_query($query);

            echo "<table>"; // start table in html

            while($row = mysql_fetch_array($result)) { //loop through results
                echo "<tr><td>" . row['onoma'] . "</td><td>" . row['epitheto'] "</td><td>" . row['bathmida'] "</td></tr>"; //$row['index'] is a filed name
            }
            echo "</table>"; //close table

            mysql_close(); //close the db connection

        ?>

    </body>
</html>

现在我知道了一个事实

  1. 连接是合法的。
  2. 查询正常,因为我对它们都进行了测试。

我还在 [so] 上建议了另一个 question,看来我错过了一些重要的东西。 HTML 中的&lt;tr&gt;&lt;td&gt; 标签设置为应有的状态,那么可能是什么问题?

顺便说一句,有没有其他方法可以稍微自动化这个过程?即开发一种可以支持更多查询等的表单?

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您忘记在下面的代码中添加 $

    while($row = mysql_fetch_array($result)) { //loop through results
                    echo "<tr><td>" . row['onoma'] . "</td><td>" . row['epitheto']     "</td><td>" . row['bathmida'] "</td></tr>"; //$row['index'] is a filed name
    }
    

    正确的代码是:

    while($row = mysql_fetch_array($result)) { //loop through results
                echo "<tr><td>" . $row['onoma'] . "</td><td>" . $row['epitheto'] "</td><td>" . $row['bathmida'] "</td></tr>"; //$row['index'] is a filed name
            }
    

    【讨论】:

    • 通过不放 $ 它给出错误但可能是你有关闭错误你需要放 error_reporting(E_ALL);在 之后的页面顶部
    • 好的,我将添加它以查看发生了什么,因为添加 $ 后它仍然没有显示任何内容
    • put error_reporting(E_ALL);在
    • 写 print_r($result);exit;在此行之后 $result = mysql_query($query);
    • 再一次没有打印出来
    【解决方案2】:

    你错过了$,你必须像这样使用:$row->onoma

    echo "<tr><td>" . $row->onoma . "</td><td>" . $row->epitheto"</td></tr>
    

    【讨论】:

      猜你喜欢
      • 2015-04-14
      • 1970-01-01
      • 2016-01-02
      • 2015-10-22
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      • 2018-03-27
      • 2015-03-28
      相关资源
      最近更新 更多