在php中查看数据表中的所有错误[关闭]

When i run it. i only see 1 records of customer and it says that the error is in " WHILE($cus = mysql_fetch_array($cus)){" Line.. anyone knows how to solve it? ..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>

you can't use $cus as the result set for the query and the row information. one of these variables needs to be something different. in my comment above, I suggested turning one into row but it's a lot easier to just change the name of the result set, so that is what I did in the code below.

I assume you have the correct connection set up before your pasted code starts, too.

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

Keep in mind, mysql functions are old and will be removed from PHP. You should not waste your time learning them. Instead use PDO or MYSQLI. Personally, I think PDO is easier. If you decide to switch over to one of those two new DB classes, be sure to parameterize your queries and bind values to help prevent SQL injection.

Please close your missing while loop curly braces.

Complete code:-

    <?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 } ?>

You are missing the closing statement for your while loop. Add this to the bottom

<?php } ?>

Second issue

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

Change the variable name there

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

You get from $cus, and assign it to $cus as well. This means you will see 1 record, and afterwards it fails on the next fetch since you changed the context:

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

Would be better :)

What about this code:

<?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 } ?>

The code is clean, condition is properly checked against FALSE and no need for useless lines of code...