2014-12-29 23 views
4

所有表行我做了一个HTML表,我能够从MySQL表中检索数据。但是,我只能显示最后一个注册表。我想展示给大家。显示在<table>

我的代码:

<?php 

$con = mysql_connect("localhost","x","x"); 

if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("x", $con); 


$query = "SELECT * FROM noticias"; 

$comments = mysql_query($query); 


while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) 
{ 
    $name = $row['nome']; 
    $email = $row['email']; 
    $website = $row['lugar']; 
    $comment = $row['comment']; 
    $timestamp = $row['data']; 

    $name = htmlspecialchars($row['nome'],ENT_QUOTES); 
    $email = htmlspecialchars($row['email'],ENT_QUOTES); 
    $website = htmlspecialchars($row['lugar'],ENT_QUOTES); 
    $comment = htmlspecialchars($row['comment'],ENT_QUOTES); 

} 

mysql_close($con); ?> 




    <table class="heavyTable"> 
     <thead> 
     <tr> 
      <th>Nome</th> 
      <th>E-mail</th> 
      <th>Lugar</th> 
      <th>Notícia</th> 
      <th>Data</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr> 
      <td><?php echo $name ?></td> 
      <td>$email</td> 
      <td>$website</td> 
      <td>$comment</td> 
      <td>$timestamp</td> 
     </tr> 
     </tbody> 
    </table> 

正如你所看到的,现在我只是想一行。到目前为止,我正在显示最后一个注册表。我想向他们展示一切,我该怎么做?

+0

你在表中有多个记录呢? –

+4

您需要在循环中发出表行。 –

+0

使用',而不是'mysql_' – lolbas

回答

2

这种尝试,你会得到你在while循环

<?php 

$con = mysql_connect("localhost","x","x"); 

if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("x", $con); 


$query = "SELECT * FROM noticias"; 

$comments = mysql_query($query); 
?> 
<table class="heavyTable"> 
    <thead> 
    <tr> 
     <th>Nome</th> 
     <th>E-mail</th> 
     <th>Lugar</th> 
     <th>Notícia</th> 
     <th>Data</th> 
    </tr> 
    </thead> 

<?php 

while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) 
{ 
$name = $row['nome']; 
$email = $row['email']; 
$website = $row['lugar']; 
$comment = $row['comment']; 
$timestamp = $row['data']; 

$name = htmlspecialchars($row['nome'],ENT_QUOTES); 
$email = htmlspecialchars($row['email'],ENT_QUOTES); 
$website = htmlspecialchars($row['lugar'],ENT_QUOTES); 
$comment = htmlspecialchars($row['comment'],ENT_QUOTES); 



mysql_close($con); ?> 




     <tbody> 
    <tr> 
     <td><?php echo $name ?></td> 
     <td>$email</td> 
     <td>$website</td> 
     <td>$comment</td> 
     <td>$timestamp</td> 
    </tr> 
<?php }?> 
    </tbody> 
</table> 
+0

谢谢mysqli_'或PDO为人它的工作。谢谢你们 !! –

+0

我感谢你的帮助,但遗憾的是我不能投你的答案,因为我没有足够的代表。 :(>反正是有,我可以让最后一个注册表首次证明? –

+0

确定没有问题的兄弟 –

2

试试这个:

<?php 

$con = mysql_connect("localhost","x","x"); 

if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("x", $con); 


$query = "SELECT * FROM noticias"; 

$comments = mysql_query($query); 

?> 
<table class="heavyTable"> 
     <thead> 
     <tr> 
      <th>Nome</th> 
      <th>E-mail</th> 
      <th>Lugar</th> 
      <th>Notícia</th> 
      <th>Data</th> 
     </tr> 
     </thead> 
     <tbody> 
<?php 
while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) 
{ 
    $name = $row['nome']; 
    $email = $row['email']; 
    $website = $row['lugar']; 
    $comment = $row['comment']; 
    $timestamp = $row['data']; 

    $name = htmlspecialchars($row['nome'],ENT_QUOTES); 
    $email = htmlspecialchars($row['email'],ENT_QUOTES); 
    $website = htmlspecialchars($row['lugar'],ENT_QUOTES); 
    $comment = htmlspecialchars($row['comment'],ENT_QUOTES); 
?> 
     <tr> 
      <td><?php echo $name ?></td> 
      <td>$email</td> 
      <td>$website</td> 
      <td>$comment</td> 
      <td>$timestamp</td> 
     </tr> 
<?php } 

mysql_close($con); ?> 


     </tbody> 
    </table> 
3
 <?php 
$con = mysql_connect("localhost","x","x"); 
if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("x", $con); 


$query = "SELECT * FROM noticias"; 

$comments = mysql_query($query); 


echo "<table border='1'> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
</tr>"; 

while($row = mysqli_fetch_array($comments)) 
{ 
echo "<tr>"; 
echo "<td>" . $row['nome'] . "</td>"; 
echo "<td>" . $row['email'] . "</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 

mysqli_close($con); 
?> 

做这样的事情

1
<table class="heavyTable"> 
     <thead> 
     <tr> 
      <th>Nome</th> 
      <th>E-mail</th> 
      <th>Lugar</th> 
      <th>Notícia</th> 
      <th>Data</th> 
     </tr> 
     </thead> 
     <tbody> 

<?php 

$con = mysql_connect("localhost","x","x"); 

if (!$con) 
{ 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("x", $con); 


$query = "SELECT * FROM noticias"; 

$comments = mysql_query($query); 


while($row = mysql_fetch_array($comments, MYSQL_ASSOC)) 
{ 
    $name = $row['nome']; 
    $email = $row['email']; 
    $website = $row['lugar']; 
    $comment = $row['comment']; 
    $timestamp = $row['data']; 

    $name = htmlspecialchars($row['nome'],ENT_QUOTES); 
    $email = htmlspecialchars($row['email'],ENT_QUOTES); 
    $website = htmlspecialchars($row['lugar'],ENT_QUOTES); 
    $comment = htmlspecialchars($row['comment'],ENT_QUOTES); 

     echo '<tr>'; 
     echo '<td>'.$name.'</td>' 
       SAME ALL FIELDS 
      .... 
     echo '</tr>'; 

} 

mysql_close($con); ?> 




     </tbody> 
    </table> 

尝试犯了一个错误以上。

+0

感谢的人,我很欣赏你的帮助,但遗憾的是我不能投你的答案,因为我没有足够的代表。:( –

+0

我想,你现在应该做。 –