【问题标题】:How to pull integer from mySQL database and display in HTML document [duplicate]如何从 mySQL 数据库中提取整数并在 HTML 文档中显示 [重复]
【发布时间】:2020-05-31 20:47:38
【问题描述】:

我正在尝试编写一个可以使用 PHP 显示来自 mySQL 数据库的信息的网站。

我已经建立了数据库连接,我相信它正在执行查询但它没有显示查询。在代码中回显第一个变量后,它将停止显示数据。

我直接在 mySQL 中尝试了查询,它给了我想要的响应。

这是 HTML 文档的代码:

编辑:我试图显示的值是一个整数。

<!DOCTYPE html>
<html>
    <head>
        <!-- Start Link Stylesheet -->
        <link rel="stylesheet" type="text/css" href="style.css" />
        <!-- End Link Stylesheet-->

        <!-- Start Meta Tags -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta charset="utf-8">
        <meta author="Cole K. Motuliak">
        <!-- End Meta Tags -->

        <title>sumstats - view stats</title>
    </head>

    <body>
        <header>
            <h1 class="center" id="bigger">JHS IT Summer Statistics Viewpage</h1>
        </header>
            <h1 class="center">Welcome</h1>
            <div class="center">
                <?php
                include 'view_stats.php';
                ?>

        </div>     


        <footer>
            <p class="center">&copy; 2020 Cole K. Motuliak | <a href="mailto:cmotuliak3@gmail.com">cmotuliak3@gmail.com</a></p>
        </footer>
    </body>










</html>

这是 PHP 代码

<?php
                $servername = "localhost:3306"; // Define Server Domain Name / TCP port
                $username = "sumstats_viewonly"; // Define username
                $password = "*****"; // Define password, PASSWORD CHANGED FROM SCRIPT
                $dbname = "sumstats_db"; // Define database name

                // Create connection
                $conn = new mysqli($servername, $username, $password, $dbname);
                // Check connection
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                }
                $imaged_query = "SELECT imaged FROM sumstats_table";
                $tagged_query = "SELECT tagged FROM sumstats_table";
                $unboxed_query = "SELECT unboxed FROM sumstats_table";
                $imaged_int = $conn->query($imaged_query);
                $tagged_int = $conn->query($tagged_query);
                $unboxed_int = $conn->query($unboxed_query);
                echo "<p >Imaged Count:", $imaged_int, "EndTest</p>";
                echo "<p >Tagged Count:", $tagged_int, "EndTest</p>";
                echo "<p >Unboxed Count:", $unboxed_int, "EndTest</p>";
                $conn->close();


                ?>

如果有人可以帮助并告诉我我做错了什么,请告诉我。谢谢!

【问题讨论】:

  • 你可以在这里运行for或while循环,打印数据库中的所有数据
  • 另外,请确保您的错误报告已打开。您应该会收到消息。

标签: php html mysql


【解决方案1】:

您可以将其用作您的 view_stats.php

它减少了数据库查询并允许循环

<?php
$servername = "localhost:3306"; // Define Server Domain Name / TCP port
$username = "sumstats_viewonly"; // Define username
$password = "*****"; // Define password, PASSWORD CHANGED FROM SCRIPT
$dbname = "sumstats_db"; // Define database name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = $conn->query("SELECT imaged,tagged,unboxed FROM sumstats_table");
while($array = mysqli_fetch_assoc($sql)){
    echo "<p >Imaged Count:", $array['images'], "EndTest</p>";
    echo "<p >Tagged Count:", $array['tagged'], "EndTest</p>";
    echo "<p >Unboxed Count:", $array['unboxed'], "EndTest</p>";
}

$conn->close();

【讨论】:

    【解决方案2】:

    您需要从结果资源中获取数据。由于这些都是同一个表中的简单列,因此您也可以在单个查询中获取它们,如下所示:

    $just_one_query = "SELECT imaged, tagged, unboxed FROM sumstats_table";
    $result = $conn->query($just_one_query);
    $counts = $result->fetch_assoc(); // Here the magic.
    
    echo "<p >Imaged Count:", $counts['imaged'], "EndTest</p>";
    echo "<p >Tagged Count:", $counts['tagged'], "EndTest</p>";
    echo "<p >Unboxed Count:", $counts['unboxed'], "EndTest</p>";
    

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多