在php中查看所有数据表时出错


Error to view all in data table in php

当我运行它时。我只看到客户的1条记录,它说错误在"WHILE($cus=mysql_fetch_array($cus)){"行中。有人知道如何解决它吗?…tnx

<table id="datatables" class="display">
    <thead>
        <tr>
            <th>ID</th>
            <th>FullName</th>
            <th>Age</th>
            <th>Gender</th>
            <th>Email</th>
            <th>Barangay</th>
            <th>CompleteAddress</th>
            <th>Username</th>
            <th>Password</th>
        </tr>
    </thead>
    <tbody>
        <?php $cus = mysql_query("SELECT * FROM customer") or die(mysql_error()); ?>
        <?php
        WHILE ($cus = mysql_fetch_array($cus)) {
            $id = $cus['cus_id'];
            $email = $cus['email'];
            $control = $cus['cus_id'];
            $user = $cus['username'];
            $pass = $cus['password'];
            $brgy = $cus['barangay'];
            $comadd = $cus['com_address'];
            $age = $cus['age'];
            $gend = $cus['gender'];
            $fname = $cus['firstname'] . "&nbsp;" . $cus['middlename'] . "&nbsp;" . $cus['lastname'];
            ?>
            <tr class="gradeA del<?php echo $id; ?>">
                <td><?php echo $control; ?></td>
                <td><?php echo $fname; ?></td>
                <td><?php echo $age; ?></td>
                <td><?php echo $gend; ?></td>
                <td><?php echo $email; ?></td>
                <td><?php echo $brgy; ?></td>
                <td><?php echo $comadd; ?></td>
                <td><?php echo $user; ?></td>
                <td><?php echo $pass; ?></td>
            </tr>
            <?php
        }
        ?>
    </tbody>
</table>

您缺少while循环的结束语句。将此添加到底部

<?php } ?>

第二期

while($cus = mysql_fetch_array($cus)) {  // You are overwriting the recordset

在中更改变量名称

while($cusTemp = mysql_fetch_array($cus)) {  // or any other name convenient

请关闭丢失的while循环大括号。

完整代码:-

    <?php 
    while($rows = mysql_fetch_array($cus)) {
            $id = $rows['cus_id'];
            $email = $rows['email'];
            $control = $rows['cus_id'];
            $user = $rows['username'];
            $pass = $rows['password'];
            $brgy = $rows['barangay'];
            $comadd = $rows['com_address'];
            $age = $rows['age'];
            $gend = $rows['gender'];
            $fname = $rows['firstname'] . "&nbsp;" . $rows['middlename']. "&nbsp;" . $rows['lastname'];
            ?>
            <tr class="gradeA del<?php echo $id;?>">
                    <td><?php echo $control; ?></td>
                    <td><?php echo $fname; ?></td>
                    <td><?php echo $age; ?></td>
                    <td><?php echo $gend; ?></td>
                    <td><?php echo $email; ?></td>
                    <td><?php echo $brgy; ?></td>
                    <td><?php echo $comadd; ?></td>
                    <td><?php echo $user; ?></td>
                    <td><?php echo $pass; ?></td>
            </tr>
    <?php } ?>

不能使用$cus作为查询和行信息的结果集。其中一个变量需要有所不同。在我上面的评论中,我建议把一个变成一行,但只更改结果集的名称要容易得多,所以我在下面的代码中就是这么做的。

我想在粘贴的代码开始之前,您也已经设置好了正确的连接。

<table id="datatables" class="display">
    <thead>
        <tr>
            <th>ID</th>
            <th>FullName</th>
            <th>Age</th>
            <th>Gender</th>
            <th>Email</th>
            <th>Barangay</th>
            <th>CompleteAddress</th>
            <th>Username</th>
            <th>Password</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        // this used to be $cus but I renamed it... 
        $customer_results = mysql_query("SELECT * FROM customer") or die(mysql_error());  
        //  here was the issue... you were using $cus twice.  
        WHILE ($cus = mysql_fetch_array($customer_results)) 
        { 
            $id = $cus['cus_id'];
            $email = $cus['email'];
            $control = $cus['cus_id'];
            $user = $cus['username'];
            $pass = $cus['password'];
            $brgy = $cus['barangay'];
            $comadd = $cus['com_address'];
            $age = $cus['age'];
            $gend = $cus['gender'];
            $fname = $cus['firstname'] . "&nbsp;" . $cus['middlename'] . "&nbsp;" . $cus['lastname'];
            ?>
            <tr class="gradeA del<?php echo $id; ?>">
                <td><?php echo $control; ?></td>
                <td><?php echo $fname; ?></td>
                <td><?php echo $age; ?></td>
                <td><?php echo $gend; ?></td>
                <td><?php echo $email; ?></td>
                <td><?php echo $brgy; ?></td>
                <td><?php echo $comadd; ?></td>
                <td><?php echo $user; ?></td>
                <td><?php echo $pass; ?></td>
            </tr>
            <?php
        }
        ?>
    </tbody>
</table>

请记住,mysql函数是旧的,将从PHP中删除。你不应该浪费时间去学习它们。而是使用PDO或MYSQLI。就我个人而言,我认为PDO更容易。如果您决定切换到这两个新的DB类中的一个,请确保参数化查询和绑定值,以帮助防止SQL注入。

您从$cus获得,并将其分配给$cus。这意味着您将看到1条记录,之后由于您更改了上下文,它在下一次提取时失败:

while($cusdata = mysql_fetch_assoc($cus)){

会更好:)

这个代码怎么样:

<?php while (($row = mysql_fetch_array($cus)) !== false) { ?>
    <tr>
        <tr class="gradeA del<?php echo $row['cus_id'];?>">
        <td><?php echo $row['cus_id']; ?></td>
        <td><?php echo $row['firstname'] . "&nbsp;" . $row['middlename']. "&nbsp;" . $row['lastname']; ?></td>
        <td><?php echo $row['age']; ?></td>
        <td><?php echo $row['gender']; ?></td>
        <td><?php echo $row['email']; ?></td>
        <td><?php echo $row['barangay']; ?></td>
        <td><?php echo $row['com_address']; ?></td>
        <td><?php echo $row['username']; ?></td>
        <td><?php echo $row['password']; ?></td>
    </tr>
<?php } ?>

代码是干净的,根据FALSE正确检查了条件,不需要无用的代码行。。。