Php 如果在表名之后使用任何条件,则表的最后一行不会显示

Php 如果在表名之后使用任何条件,则表的最后一行不会显示,php,html,mysql,Php,Html,Mysql,我想显示表中的所有数据 但是,如果我使用/addORDER BY id DESC或$sql=“SELECT*FROM$tbl\u name之后的任何代码,则最后一行不会显示 <?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

我想显示表中的所有数据

但是,如果我使用/add
ORDER BY id DESC
$sql=“SELECT*FROM$tbl\u name
之后的任何代码,则最后一行不会显示

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

?>

当然,当您使用
DESC
时,它从最高ID开始。然后调用:

$ro = mysql_fetch_array($result); // this is the first row.
它获取第一行

然后循环:
while($row=mysql\u fetch\u array($result))
从第二行开始

所以只需删除这个
$ro=mysql\u fetch\u数组($result);
不需要的获取行

强制性说明:

。它们不再被维护。请参阅?改为了解,并使用或-将帮助您决定使用哪个。如果您选择PDO

PDO使用示例:

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

?>

当然,当您使用
DESC
时,它从最高ID开始。然后调用:

$ro = mysql_fetch_array($result); // this is the first row.
它获取第一行

然后循环:
while($row=mysql\u fetch\u array($result))
从第二行开始

所以只需删除这个
$ro=mysql\u fetch\u数组($result);
不需要的获取行

强制性说明:

。它们不再被维护。请参阅?改为了解,并使用或-将帮助您决定使用哪个。如果您选择PDO

PDO使用示例:

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

?>

在循环之前有一个额外的
mysql\u fetch\u数组($result);

在循环之前有一个额外的
mysql\u fetch\u数组($result);

必须正确地生成Sql查询字符串。如下:

$sql = "SELECT * FROM ".$tbl_name." ORDER BY id DESC";
在Php中有两个字符串运算符。第一个是连接运算符('.'),它返回左右参数的连接。第二个是连接赋值运算符('.='),它将右侧的参数附加到左侧的参数

更多信息->


希望有帮助。

您必须正确地生成Sql查询字符串。如下所示:

$sql = "SELECT * FROM ".$tbl_name." ORDER BY id DESC";
在Php中有两个字符串运算符。第一个是连接运算符('.'),它返回左右参数的连接。第二个是连接赋值运算符('.='),它将右侧的参数附加到左侧的参数

更多信息->


希望有帮助。

这并不能回答这个问题。要评论或要求作者澄清,请在他们的帖子下方留下评论-你可以随时对自己的帖子发表评论,一旦你有足够的评论,你就可以发表评论。谢谢你提供的信息。问题是我现在没有足够的代表发表评论。而且我认为这足以让提问者看到他们做错了什么。好吧,无论如何,再次感谢。那么最好删除这一条?@SidM为什么这不是问题的答案?这与Ghost给出的答案基本相同,尽管要简洁得多。这并不能提供问题的答案。批评或要求澄清对于作者,在他们的帖子下方留下评论-你可以随时对自己的帖子发表评论,一旦你有足够的评论,你就可以发表评论。谢谢你提供的信息。问题是我现在没有足够的代表发表评论。我想这足以让提问者看到他们做错了什么。好吧,无论如何,再次感谢.那么最好删除这个?@SidM为什么这不是问题的答案?它基本上与Ghost给出的答案相同,尽管要简洁得多。