1

I want to show all data from my table.

But if I use/add ORDER BY id DESC or any code after $sql="SELECT * FROM $tbl_name, then last row is not showing.

<?php

include "db.php";

$tbl_name="report"; // Table name 

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";

$result=mysql_query($sql);
$count=mysql_num_rows($result);
$ro = mysql_fetch_array($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count>=1) {

    echo "<table border='1' align='center' cellpadding='10'>
    <tr>
    <th>Reporter</th>
    <th>Message</th>
    <th>Reporter Ip Address</th>
    <th>Action</th>
    </tr>";

    while($row = mysql_fetch_array($result)) {

        echo "<tr>";
        echo "<td>" . $row['from'] . "</td>";
        echo "<td>" . $row['msg'] . "</td>";
        echo "<td>" . $row['to'] . "</td>";
        echo "<td class='middle'>" . $row['ip'] . "</td>";
        echo "<td><a class=\"confirmation\" href=\"report_delete.php?id=" . $row['id'] . "\">Delete</a></td>";

        echo "</tr>";

    }
    echo "</table>";

}

else {
    print "<p align='center'>Nothing found.</p>";
}

?>
Kevin
  • 41,694
  • 12
  • 53
  • 70

3 Answers3

4

Of course when you used the DESC, it starts off the highest ID. Then the invocation of:

$ro = mysql_fetch_array($result); // this is the first row.

It fetches the first row.

Then your loop: while($row = mysql_fetch_array($result)) starts off with the second row.

So just remove this $ro = mysql_fetch_array($result); unneeded fetching line.

Obligatory Note:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Sample PDO Usage:

<?php

$db = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');

$query = $db->query('SELECT * FROM report ORDER BY id DESC');
$rows = $query->fetchAll(PDO::FETCH_ASSOC);

if(count($rows) > 0) {

    echo "
        <table border='1' align='center' cellpadding='10'>
        <tr>
            <th>Reporter</th>
            <th>Message</th>
            <th>Reporter Ip Address</th>
            <th>Action</th>
        </tr>
    ";

    foreach($rows as $row) {
        echo "<tr>";
            echo "<td>" . $row['from'] . "</td>";
            echo "<td>" . $row['msg'] . "</td>";
            echo "<td>" . $row['to'] . "</td>";
            echo "<td class='middle'>" . $row['ip'] . "</td>";
            echo "<td><a class=\"confirmation\" href=\"report_delete.php?id=" . $row['id'] . "\">Delete</a></td>";
        echo "</tr>";
    }

    echo '</table>';

} else {
    echo "<p align='center'>Nothing found.</p>";
}

?>
Community
  • 1
  • 1
Kevin
  • 41,694
  • 12
  • 53
  • 70
0

You have an extra mysql_fetch_array($result); before the loop.

rasso
  • 2,131
  • 3
  • 21
  • 24
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Sid M Jan 12 '15 at 08:22
  • Thank you for the info. The problem is that I don't have sufficient rep to comment right now. And I thought that would be enough for the asker to see what they're doing wrong. Well, anyway, thanks again. Better to delete this one then? – rasso Jan 12 '15 at 08:26
  • 1
    @SidM Why isn't this an answer to the question? It's basically the same answer that Ghost gave, although considerably more terse. – Barmar Jan 12 '15 at 08:27
0

You must generete the Sql query String correctly. Like this:

$sql = "SELECT * FROM ".$tbl_name." ORDER BY id DESC";

In Php there are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side.

More info -> http://php.net/manual/en/language.operators.string.php

Hope it helps.

mgamon
  • 308
  • 7
  • 16