2014-12-29 28 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

我想,你現在應該做。 –