从数据库显示表时出错

时间:2014-06-19 03:03:21

标签: php database

我正在尝试使用PHP显示我的表,t2d_10来自数据库,chr10。我的表有5列,分别是rs1,rs2,rs3,rs4,rs5。这是我的代码:

<?php 

 // set database server access variables: 
 $host = "localhost"; 
 $user = "root"; 
 $pass = ""; 
 $db = "chr10";

 // open connection 
 $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 

 // select database 
 mysql_select_db($db, $connection) or die ("Unable to select database!"); 

 // create query 
 $query = "SELECT * FROM t2d_10"; 

 // execute query 
 $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

 // see if any rows were returned 
 if (mysql_num_rows($result) > 0) { 
     // yes 
     // print them one after another 
     echo "<table cellpadding=10 border=1>"; 
     while($row = mysql_fetch_row($result)) { 
         echo "<tr>"; 
         echo "<td>".$row['rs1']."</td>"; 
         echo "<td>".$row['rs2']."</td>"; 
         echo "<td>".$row['rs3']."</td>"; 
         echo "<td>".$row['rs4']."</td>";
         echo "<td>".$row['rs5']."</td>";
         echo "</tr>"; 
     } 
     echo "</table>"; 
 } 
 else { 
     // no 
     // print status message 
     echo "No rows found!"; 
 } 

 // free result set memory 
 mysql_free_result($result); 

 // close connection 
 mysql_close($connection); 

?>

错误:

Notice: Undefined index: rs1 in C:\wamp\www\ch\run_db.php on line 28

Notice: Undefined index: rs2 in C:\wamp\www\ch\run_db.php on line 29

Notice: Undefined index: rs3 in C:\wamp\www\ch\run_db.php on line 30

Notice: Undefined index: rs4 in C:\wamp\www\ch\run_db.php on line 31

Notice: Undefined index: rs5 in C:\wamp\www\ch\run_db.php on line 32

我对这个PHP很新。有人可以帮我这个吗?提前谢谢。

2 个答案:

答案 0 :(得分:2)

语法突出显示和错误消息都告诉您确切的错误。

您在代码中遗漏了一些"字符。

答案 1 :(得分:0)

mysql_fetch_row()将只是一个索引数组。

使用mysql_fetch_array()mysql_fetch_assoc()

请检查:Difference between mysql_fetch_array and mysql_fetch_row?