2014-06-19 34 views
0

我正在嘗試使用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。任何人都可以幫助我嗎?提前致謝。

+0

'mysql_select_db($連接)'一兩件事。 –

+0

@ Fred-ii-:不,那會是錯誤的。 OP的是正確的。 http://www.php.net/manual/en/function.mysql-select-db.php –

+0

@LightnessRacesinOrbit Duh,對不起。我的錯誤哈​​哈我的意思是'mysql_select_db($ db,$連接)' –

回答

2

語法高亮顯示和錯誤消息都告訴你到底發生了什麼問題。

您錯過了代碼中的一些"字符。

+0

OP修改了缺少'''的問題,想知道爲什麼編輯:現在未定義索引 –

+0

謝謝,@LightnessRacesinOrbit。我已經編輯過了。錯誤在$ result。未定義的索引rs1,rs2,rs3,rs4,rs5。我應該用什麼來代替? – nOOr

+1

我不打算逐個解決你的問題,所以不是幫助臺。聊天室幫助你:) –

相關問題