0

I'm trying to echo a count result from my MySQL db in PHP, so it looks like

NUMBER OF STUDENTS

[# count from db]

but nothing is coming up on my HTML page. I ran this store procedure in my db and I'm getting 923 so I know it's not supposed to be empty. However, when I run this on the HTML page, it falls into the "Nothing exists" case. I printed to the console and for some reason, it returns empty.

I feel like this might be with this particular php section, because I have another SQL query I run somewhere else in the code and it works just fine. I'm not sure why it's returning nothing:

<body>
//////// THIS CALL WORKS FINE AND DISPLAYS RESULTS ////////
<h5>Here is a sample of the first 10 students pulled from the database:</h5>
            <center><?php
            $sql="call DisplayRandomTable('db', 'table', 10)";
            $result = mysqli_query($conn, $sql);
            $resultCheck = mysqli_num_rows($result);

            if ($resultCheck > 0) {
                echo "<table border = '2'>
                    <tr>
                        <th><center>Student ID</center></th>
                        <th><center>Student Name</center></th>
                        <th><center>Student Start Date</center></th>
                    </tr>";
                while ($row = mysqli_fetch_assoc($result)) {
                    echo "<tr>";
                    echo "<td><center>" . $row['STUDENT_ID'] ."</center></td>";
                    echo "<td><center>" . $row['Last_Name'] . "</center></td>";
                    echo "<td><center>" . $row['Start_Date'] . "</center></td>";
                    echo "</tr>";
                }
                echo "</table>";
            }
            ?></center>
////////////////////////////
.
.
.
.
//////// THIS CALL IS WHERE THE ISSUE IS ////////
    <div id="tabs-2" class="tabContent">
        <h3><center>STUDENT DEMOGRAPHY</center></h3>
            <div class="flex-container">
                <div class="flex-item">
                    <h4><center>NUMBER OF STUDENTS</center></h4>
                        <?php
                            // Check connection
                            if ($conn->connect_error) {
                                die("Connection failed: " . $conn->connect_error);
                            }

                            $sql = "call GetTableCount('Students_Information', 'Students')";
                            $result = $conn->query($sql);
                            echo("<script>console.log('The results: " . $result . "');</script>");
                            if ($result->num_rows > 0) {
                                $row = $result->fetch_row();
                                echo 'Total:'. $row[0]; // print total record
                            } 
                            else 
                            {
                                echo 'Nothing exists';
                            }
                        ?>
                    </div>
                </div>
        </div>
</body>
Djikstra's
  • 13
  • 4
  • I think rename this line `$sql = "call GetTableCount('Students_Information', 'Students')";` to `$sql = "SELECT GetTableCount('Students_Information', 'Students');";` And put a `printf("Error message: %s\n", $conn->error);` after your line `echo 'Nothing exists';`. Maybe is a error in mysql syntax. – MitnickCodeHelper May 31 '21 at 20:01
  • Ok I tried to do that and I get this printed on my HTML page: Nothing exists Error message: Commands out of sync; you can't run this command now – Djikstra's May 31 '21 at 20:09
  • Here an other post with the error message [Stackoverflow - Commands out of sync; you can't run this command now](https://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now) – MitnickCodeHelper May 31 '21 at 20:35
  • Or another hint. Have the user the rights to use mysql `procedures or functions`? – MitnickCodeHelper May 31 '21 at 20:39
  • So it ,ooks like it might be an issue with freeing up queries in between calls? Because the user has rights to use mysql procedures and functions, I make a call procedure a few lines before and it works fine – Djikstra's May 31 '21 at 21:40

0 Answers0