Keep getting these errors:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Projeto\all.php on line 71

Notice: Use of undefined constant total2 - assumed 'total2' in C:\xampp\htdocs\Projeto\all.php on line 72

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Projeto\all.php on line 124

Notice: Undefined variable: connect in C:\xampp\htdocs\Projeto\all.php on line 174

Warning: mysql_close() expects parameter 1 to be resource, null given in C:\xampp\htdocs\Projeto\all.php on line 174
------------------------------------------------------------------------------------------------
Really need help!
Here's the whole file!(doesnt have html, head, or body, cus i separated the files to make it easier for css.)

all.php:

<h2>List of all the users</h2>
<div class="center_float">

    <?php
    require 'conf/connection.php';

    $number = 5;

    if (isset($_GET['order'])) {
        $mode = $_GET['mode'];
        $order = $_GET['order'];
    } else {
        $mode = "UP";
        $order = "name";
    }

    function mode($order, $mode, $current) {
        if ($order == $current) {
            if ($mode == "UP") {
                $mode = "DOWN";
            } else {
                $mode = "UP";
            }
        } else {
            $mode = "UP";
        }
        return $mode;
    }

    function mode_complete($order, $mode, $current) {
        if ($order == $current) {
            if ($mode == "UP") {
                $mode_complete = "Up";
            } else {
                $mode_complete = "Down";
            }
        } else {
            $mode_complete = "Up";
        }
        return $mode_complete;
    }

    function arrow($order, $mode, $current) {
        if ($order == $current) {
            $img_id = "ord_" . $order;
            echo "<img src='img/" . $mode . ".png' id='" . $img_id . "'>";
        }
    }

    function mouse($order, $mode, $current) {
        if ($order == $current) {
            $img_id = "ord_" . $order;
            $mode_over = ($mode == "UP") ? "DOWN" : "UP";
            echo "onmouseover='order(\"" . $img_id . "\",\"" . $mode_over . "\")' ";
            $mode_out = ($mode == "UP") ? "UP" : "DOWN";
            echo "onmouseout='order(\"" . $img_id . "\",\"" . $mode_out . "\")' ";
        }
    }

    if (isset($_GET['page'])) {
        $page = (int) $_GET['page'];
        $home = ($page - 1) * $number;
    } else {
        $page = 1;
        $home = 0;
    }

    $total_selection = mysql_query("SELECT * FROM users");
    $total = mysql_num_rows($total_selection);
    $r = mysql_query("SELECT * FROM users ORDER BY $order $mode LIMIT $home, $number");
    $total2 = mysql_num_rows($r);
    $final_number2 = $home + $total2;
    $value = ceil($total / $number);
    $start_number = $home + 1;
    $final_number = $home + $number;

    if ($total == 0) {
        echo "<p>No users registered.</p>";
    } elseif ($total == 1) {
        echo "<p>There's only <b>1</b> user registered.</p>";
    }

    if ($total > 1) {
        if ($value > 1) {
            if ($total2 < $number) {
                if ($start_number == $final_number2) {
                    echo "<p>Showing <b>1</b> result from the total of <b>$total</b> separated in <b>$value</b> page.</p>";
                } else {
                    echo "<p>Showing <b>$start_number</b> - <b>$final_number2</b> results from the total of <b>$total</b> separated in <b>$value</b> pages.</p>";
                }
            } else {
                echo "<p>Showing <b>$start_number</b> - <b>$final_number</b> results from the total of <b>$total</b> separated in <b>$value</b> pages.</p>";
            }
        } else {
            echo "<p>Showing <b>$total</b> results in <b>1</b> page.</p>";
        }
    }

    if ($total) {
        if ($page >= 1 && $page <= $value) {
            ?>

            <br>
            <table class="tab_all">

                <tr>
                    <th width="30"><a href="?p=all&page=<?php echo $page ?>&order=id&mode=<?php echo mode($order, $mode, 'id') ?>" title="Order per ID <?php echo mode_complete($order, $mode, 'id') ?>" <?php echo mouse($order, $mode, 'id') ?> class="lth">ID <?php arrow($order, $mode, 'id') ?></a></th>
                    <th width="220"><a href="?p=all&page=<?php echo $page ?>&order=name&mode=<?php echo mode($order, $mode, 'name') ?>" title="Order per Name <?php echo mode_complete($order, $mode, 'name') ?>" <?php echo mouse($order, $mode, 'name') ?> class="lth">Name <?php arrow($order, $mode, 'name') ?></a></th>
                    <th width="70"><a href="?p=all&page=<?php echo $page ?>&order=username&mode=<?php echo mode($order, $mode, 'username') ?>" title="Order per Username <?php echo mode_complete($order, $mode, 'username') ?>" <?php echo mouse($order, $mode, 'username') ?> class="lth">Username <?php arrow($order, $mode, 'username') ?></th>
                    <th width="70"><a href="?p=all&page=<?php echo $page ?>&order=password&mode=<?php echo mode($order, $mode, 'password') ?>" title="Order per Password <?php echo mode_complete($order, $mode, 'password') ?>" <?php echo mouse($order, $mode, 'password') ?> class="lth">Password <?php arrow($order, $mode, 'password') ?></th>
                    <th width="170"><a href="?p=all&page=<?php echo $page ?>&order=email&mode=<?php echo mode($order, $mode, 'email') ?>" title="Order per E-mail <?php echo mode_complete($order, $mode, 'email') ?>" <?php echo mouse($order, $mode, 'email') ?> class="lth">E-mail <?php arrow($order, $mode, 'email') ?></th>
                    <th width="80"><a href="?p=all&page=<?php echo $page ?>&order=birth_date&mode=<?php echo mode($order, $mode, 'birth_date') ?>" title="Order per Birth Date <?php echo mode_complete($order, $mode, 'birth_date') ?>" <?php echo mouse($order, $mode, 'birth_date') ?> class="lth">Birth Date <?php arrow($order, $mode, 'birth_date') ?></th>
                    <th width="80"><a href="?p=all&page=<?php echo $page ?>&order=register_date&mode=<?php echo mode($order, $mode, 'register_date') ?>" title="Order per Register Date <?php echo mode_complete($order, $mode, 'register_date') ?>" <?php echo mouse($order, $mode, 'register_date') ?> class="lth">Register Date <?php arrow($order, $mode, 'register_date') ?></th>
                    <th width="50">Edit</th>
                    <th width="50">Delete</th>
                </tr>

                <?php
                $counter = 0;

                while ($info = mysql_fetch_array($r)) {
                    $class = ($counter % 2 == 0) ? 'odd' : 'even';
                    $counter++;

                    $birth_date = explode("-", $info['birth_date']);
                    $birth_date = $birth_date[2] . '-' . $birth_date[1] . '-' . $birth_date[0];

                    $register_date = explode("-", $info['register_date']);
                    $register_date = $register_date[2] . '-' . $register_date[1] . '-' . $register_date[0];

                    echo "<tr class=\"$class\">";
                    echo "<td>" . $info['id'] . "</td>";
                    echo "<td class=\"left\">" . $info['name'] . "</td>";
                    echo "<td>" . $info['username'] . "</td>";
                    echo "<td>" . $info['password'] . "</td>";
                    echo "<td>" . $info['email'] . "</td>";
                    echo "<td>" . $birth_date . "</td>";
                    echo "<td>" . $register_date . "</td>";
                    echo "<td><a href='?p=edit&id=" . $info['id'] . "' title='Edit'><img src='img/edit.png'></a></td>";
                    echo "<td><a href='conf/delete.php?id=" . $info['id'] . "&action=delete' title='Delete'><img src='img/delete.png' onclick='return confirm(\"Are you sure you want to delete " . $info['name'] . "?\")'></a></td>";
                    echo "</tr>";
                }
                ?>
            </table>
            <br>
            <?php
            $previous = $page - 1;
            $next = $page + 1;

            if ($page > 1) {
                echo '<a href="?p=all&page=' . $previous . '" class="paging">« Previous</a>';
            } else {
                echo '<span class="deactivated">« Previous</span>';
            }

            if ($value > 1) {
                for ($i = 1; $i <= $value; $i++) {
                    $class = ($i == $page) ? 'current_paging' : 'paging';
                    echo '<a href="?p=all&page=' . $i . '" class="' . $class . '">' . $i . '</a>';
                }
            }
            if ($page < $value) {
                echo '<a href="?p=all&page=' . $next . '" class="paging">Next »</a>';
            } else {
                echo '<span class="deactivated">Next »</span>';
            }
        } else {
            echo "<p>This page doesn't exist.</p>";
        }
    }
    mysql_close($connect);
    ?>

</div>

Dani AI

Generated

Short diagnosis and where to start: those three warnings almost always mean the SELECT failed and returned FALSE, so the result resource expected by mysql_num_rows()/mysql_fetch_array() is actually a boolean. In this code the most likely immediate cause is the ORDER BY fragment — SQL expects ASC/DESC, not UP/DOWN (that makes the query invalid and mysql_query() returns FALSE). The “Use of undefined constant total2” notice is produced when PHP sees a bareword (no $ or no quotes) instead of a variable/string; verify you used $total2 everywhere. The mysql_close() warning shows there’s no $connect in scope — check conf/connection.php sets that variable. (php.net)

Practical debugging steps (do these in order):

  • Open conf/connection.php and confirm it assigns the connection to the same variable you close (e.g. $connect = mysql_connect(...)) or remove the parameter when closing only if you rely on the last link.
  • Log the exact SQL you build, then immediately check the query result with mysql_error() so you see the real DB error.
  • Convert your UI sort mode into ASC/DESC before you build the SQL, and never interpolate arbitrary column names — use a whitelist. mysql_query() returns FALSE on syntax/permission errors; always test for that. (php.net)

Safe fixes — examples:

  • Map the UI mode to SQL direction and whitelist columns, cast offsets to integers:

    $dir = ($mode === 'UP') ? 'ASC' : 'DESC';
    $allowed = ['id'=>'id','name'=>'name','username'=>'username','email'=>'email','birth_date'=>'birth_date','register_date'=>'register_date'];
    $order = (isset($_GET['order']) && isset($allowed[$_GET['order']])) ? $allowed[$_GET['order']] : 'name';
    $sql = "SELECT * FROM users ORDER BY $order $dir LIMIT " . (int)$home . "," . (int)$number;
    $r = mysql_query($sql);
    if ($r === false) { error_log('SQL error: '.mysql_error().' — '.$sql); }

    This prevents syntax errors and SQL injection (ORDER BY cannot be parameterized, so whitelist it). (cheatsheetseries.owasp.org)

Longer-term: move off ext/mysql — it was deprecated and removed from newer PHP releases. Use MySQLi or PDO (both support prepared statements and safer APIs). Also wrap mysql_close() so you only call it if the connection variable exists and is a resource when using ext/mysql. During development enable full error reporting (E_ALL) so notices like the undefined-constant one show early. (php.net)

As suggested, start by fixing the connection file and then add the query error logging above; once the query error is visible you’ll have the exact MySQL message to finish the fix.

The errors are all about your mysql connection, there must be something wrong with the connection in your included file at the top. Plus your using Undefined constants, the errors are telling you whats wrong and where the errors are.

But first sort out the connection problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.