MYSQL记录未显示在PHP / HTML表中

时间:2014-11-17 01:11:41

标签: php mysql

我正在尝试在HTML表格中显示MySQL记录。我已经完成了这个过程,在我的php文档中它显示了表名,描述等,但是我的数据库中的数据没有显示。

这是我的代码:

<?php 

 //establishing connection
 mysql_connect('localhost', 'root', 'root');

 //selecting a database
 mysql_select_db('festival');

 $sql="SELECT * FROM deejay";

 $records=mysql_query($sql);

 ?>

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>

  <h2>Festival Diagram</h2>
<img src="diagram.jpg" alt="diagram">



<h1>Deejay Data</h1>
<table width="300" border="1" cellpadding="10" cellspacing="1">
 <tr>

<th>dj_id</th>
<th>name</th>
<th>country</th>
<th>info</th>

 </tr>
 <?php

 while($deejay=mysql_fetch_assoc($records)){
  echo "<tr>";
 echo "<td>" .$records['dj_id']."</td>";
 echo "<td>" .$records['name']."</td>";
 echo "<td>" .$records['country']."</td>"; 
 echo "<td>" .$records['info']."</td>";
 echo "</tr>";

 }
 ?>
</table>



 </body>
</html>

2 个答案:

答案 0 :(得分:1)

试试这个:

while($deejay=mysql_fetch_assoc($records)){
    echo "<tr>";
    echo "<td>" .$deejay['dj_id']."</td>";
    echo "<td>" .$deejay['name']."</td>";
    echo "<td>" .$deejay['country']."</td>"; 
    echo "<td>" .$deejay['info']."</td>";
    echo "</tr>";

}

答案 1 :(得分:1)

当您在此处运行代码时:$deejay=mysql_fetch_assoc($records)您将$deejay指定为当前行项目(表格中您从$records获得的下一行)。换句话说,$records仍然是整个检索到的数据,$deejay是您当前所在的行。所以你需要说:$deejay['...']而不是$record['...']

echo "<td>".$deejay['dj_id']."</td>";
echo "<td>".$deejay['name']."</td>";
echo "<td>".$deejay['country']."</td>"; 
echo "<td>".$deejay['info']."</td>";