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

时间:2013-12-10 14:31:21

标签: php arrays

当我运行它。我只看到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>

5 个答案:

答案 0 :(得分:1)

请关闭你丢失的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 } ?>

答案 1 :(得分:1)

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

<?php } ?>

第二期

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

更改变量名称

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

答案 2 :(得分:0)

您来自$cus,并将其分配给$cus。这意味着您将看到1条记录,之后在您更改上下文后的下一次提取时失败:

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

会更好:)

答案 3 :(得分:0)

您不能使用$ 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更容易。如果您决定切换到这两个新数据库类之一,请确保参数化您的查询并绑定值以帮助防止SQL注入。

答案 4 :(得分:0)

这段代码怎么样:

<?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正确检查,无需无用的代码行......