有人能解释一下我的代码出了什么问题吗?


Can someone explain what went wrong with my code?

这是我的代码

<div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <td>Id</td>
                                    <td>Articles Category</td>
                                    <td>Update</td>
                                    <td>Delete</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <?php
                                        $con = new mysqli("localhost", "root", "", "whatever");
        $sql = "SELECT id_kategori, nama_kategori FROM kategori";
        $stmt = $con->prepare($sql);
        $stmt->execute();
        $stmt->bind_result($id, $cat);
        while($stmt->fetch())
        {
            echo "<td>$id</td>";
            echo "<td>$cat</td>";
            echo "<td>Update</td>";
            echo "<td>Delete</td>";
        }
        $stmt->close();
                                    ?>
                                </tr>
                            </tbody>
                        </table>
                    </div>

结果显示了这个

"准备($ sql);支撑-> execute ();支撑-> bind_result (id、猫美元);While ($stmt->fetch()) {echo ";回声";回声";回声";} $支撑-> close ();?>

我像这样把它改成面向对象

<div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <td>Id</td>
                                    <td>Articles Category</td>
                                    <td>Update</td>
                                    <td>Delete</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <?php 
                                        include 'connection.php'; 
                                        $showall = new connection(); 
                                        $showall->category(); 
                                    ?>
                                </tr>
                            </tbody>
                        </table>
                    </div>

我的结果就是这个

类别();?>

谁能解释一下我的代码出了什么问题?

首先你使用了非常糟糕的编码标准。你只是在HTML中间写db连接。你可以把HTML写成

echo '<div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <td>Id</td>
                                    <td>Articles Category</td>
                                    <td>Update</td>
                                    <td>Delete</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>';
                                    <?php
                                        $con = new mysqli("localhost", "root", "", "whatever");
        $sql = "SELECT id_kategori, nama_kategori FROM kategori";
        $stmt = $con->prepare($sql);
        $stmt->execute();
        $stmt->bind_result($id, $cat);
        while($stmt->fetch())
        {
            echo "<td>$id</td>";
            echo "<td>$cat</td>";
            echo "<td>Update</td>";
            echo "<td>Delete</td>";
        }
        $stmt->close();
                                    ?>
                                echo '</tr>
                            </tbody>
                        </table>
                    </div>';