将mysql结果显示到表中

时间:2017-05-17 11:06:55

标签: php mysql

我想将myql表显示在我页面的表中。一切正常,但顺序是反向的,第一行是20,然后是19,18,17等等。有人能帮助我吗?

      <?php
$id = mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("angajati", $id) or die('Could not select db: ' . mysql_error());
$query1 = "SELECT * FROM angajati ";
$result = mysql_query($query1) or die('Error querying database.');

echo "<table summary='text' cellpadding='0' cellspacing='0'>
<thead>
<tr>
<th>ID</th>
<th>Nume</th>
<th>Prenume</th>
</tr>
</thead>
<tbody> 
";

while($row = mysql_fetch_array($result) )
{
echo "<tr class='dark'>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Nume'] . "</td>";
echo "<td>" . $row['Prenume'] . "</td>";
}
echo "</tbody> </table>";
mysql_close();
?>

1 个答案:

答案 0 :(得分:1)

1)按ID asc

使用订单
`SELECT * FROM angajati order by ID asc`

2)缺少tr关闭

  

警告mysql_query,mysql_fetch_array,mysql_connect等..扩展在PHP 5.5.0中已弃用,并且已在PHP 7.0.0中删除。   相反,应该使用MySQLi或PDO_MySQL扩展。

尝试使用mysqli_ *

          <?php
        $id = mysqli_connect("localhost","root","") or die('Could not connect: ' . mysqli_connect_error());
        mysqli_select_db( $id,"angajati") or die('Could not select db: ' . mysqli_error());

        $query1 = "SELECT * FROM angajati order by ID ASC";

        $stmt =  $id->prepare($query1);
        $stmt->execute();
        $result = $stmt->get_result();
        $count = $result->num_rows;

        echo "<table summary='text' cellpadding='0' cellspacing='0'>
        <thead>
        <tr>
        <th>ID</th>
        <th>Nume</th>
        <th>Prenume</th>
        </tr>
        </thead>
        <tbody> 
        ";
        if($count>0){
            while($row = $result->fetch_assoc())
            {
            echo "<tr class='dark'>";
            echo "<td>" . $row['ID'] . "</td>";
            echo "<td>" . $row['Nume'] . "</td>";
            echo "<td>" . $row['Prenume'] . "</td>";
            echo "</tr>";
            }
        }
        echo "</tbody> </table>";
        mysqli_close($id);
        ?>