dpdhf02040 2015-03-22 04:09
浏览 61
已采纳

如何从mysql表中选择数据,然后将其放入html表中

I'd like to draw data from a table in my mysql, and put the data into a customized table. I would like to have the info into the table as http://gyazo.com/c6a40fcd6e4edc66f7f11650f641dae2. However when I'm trying it turns out very bad. Heres my coding.

<?php
$servername = "#";
$username = "#";
$password = "#";
$dbname = "#";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT minecraft_user, service FROM votes";
$result = $conn->query($sql);
         echo '<div class="grid-40 vote-leaderboard"> <h3><i class="fa fa-star"></i>Recent Votes</h3> <table class="top-voters"> <tbody>';

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo '<tr><td>' . $row[minecraft_user]. '</td><td>' . $row[service]. '</td></tr><br>';
     }
} else {
     echo "No recent votes!";
}
         echo '</tbody>';

$conn->close();
?>

Can someone please help me with this? I've been testing for hours with no result.

  • 写回答

1条回答 默认 最新

  • doufei2662 2015-03-22 04:30
    关注

    If you want to get the last 10 table entries then you should change your sql query like this

    $sql = "SELECT minecraft_user, service FROM votes order by `minecraft_user` DESC limit 0,10";
    

    If you add id as auto_increment then

    $sql = "SELECT id, minecraft_user, service FROM votes order by `id` DESC limit 0,10";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?