0

I am trying to write a website that can display information from a mySQL database using PHP.

I have set up the database connection, I believe it is doing the query but it is not displaying the query. It stops displaying data after the first variable is echoed in the code.

I attempted the query directly in mySQL and it gave me the response I wanted.

Here is the code for the HTML document:

EDIT: The value I am trying to display is an integer.

<!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>

Here is the PHP code

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


                ?>

If anybody could assist and tell me what I am doing wrong, please let me know. Thanks!

Strawberry
  • 33,750
  • 13
  • 40
  • 57

2 Answers2

1

You can use this as your view_stats.php

It makes less database query and allows looping

<?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();
Emre Rothzerg
  • 289
  • 1
  • 5
0

You need to fetch the data from the result resource. Since these are all simply columns in the same table, you can also fetch them all in a single query, as follows:

$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>";
Markus AO
  • 4,771
  • 2
  • 18
  • 29