2013-04-01 61 views
2

好的,如標題所示,我很難弄清楚如何讓我的表格顯示爲網頁。在HTML/PHP中顯示MySQL表格時出現問題

其他一切工作正常。我已經能夠使用表單將記錄寫入表格,但我無法顯示錶格。

在此先感謝!

這裏是我的代碼:

$host = "***"; 
$userName = "***"; 
$passWord = "***"; 
$db = "doctorWho"; 

mysql_connect($host,$userName,$passWord); 
mysql_select_db($db) or die("Unable to access database"); 
$query = "SELECT * FROM patients"; 

$result = mysql_query($query); 

echo "<table border='1'> 
    <tr> 
    <th>Last Name</th> 
    <th>First Name</th> 
    <th>Address</th> 
    <th>Age</th> 
    <th>Sex</th> 
    <th>Marital Status</th> 
    <th>Medication</th> 
    <th>Date Rx'd</th> 
    <th>Quantity</th> 
    </tr>"; 

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['lastName'] . "</td>"; 
    echo "<td>" . $row['firstName'] . "</td>"; 
    echo "<td>" . $row['address'] . "</td>"; 
    echo "<td>" . $row['age'] . "</td>"; 
    echo "<td>" . $row['sex'] . "</td>"; 
    echo "<td>" . $row['maritalStatus'] . "</td>"; 
    echo "<td>" . $row['medication'] . "</td>"; 
    echo "<td>" . $row['medsWhen'] . "</td>"; 
    echo "<td>" . $row['medsQuant'] . "</td>"; 
    echo "</tr>"; 

    } 
echo "</table>"; 
+4

不要使用mysql_ *?它已被棄用。 mysqlpi或PDO是城裏的替代品和最新的野獸。 –

+0

如果你使用mysql比請不要這樣做..原因正是@Ed Heal先生告訴形式更多信息檢查http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql - 功能在PHP/14110189#14110189 –

+0

@NullPonyPointer - 你讓我聽起來像一匹馬Wilber –

回答

4

你想mysql_fetch_array()。目前,您正在混合兩種不同的擴展名。每個都有不同的東西,與擴展相關。除此之外,您應該考慮PDO或MySQLi,這是您嘗試使用的錯誤功能的來源。 This article應該可以幫助你決定你可以使用哪一種。

+0

+1看起來op是讓我們傻瓜...多麼糟糕的一段代碼 –

+0

@NullPonyPointer其實,我只是一個noob在這,慢慢學習。儘管感謝您的幫助! – Pachi

+0

@Pachi如果此答案解決了您的問題,請考慮通過點擊旁邊向下箭頭旁的複選標記來接受它。 – Daedalus

0
// I think your connection should look like this 
$conn = mysql_connect($host,$userName,$passWord); 
mysql_select_db($conn,$db) or die("Unable to access database"); 

// another is your using mysql as connection so you can use mysqli functions on that rofl. 
//Instead use mysql functions 
mysql_fetch_array(); 

注:另一種是不使用@標誌處理錯誤。更好的做法是使用.. ini_set()或配置php.ini來處理顯示錯誤。

+0

請不要使用['@'](http:// php。net/manual/zh/language.operators.errorcontrol.php)使用這個 –

+0

@NullPonyPointer的錯誤做法對不起,我回答了這個問題..大聲笑.. –

0

from php ref。

mixed mysqli_fetch_array (mysqli_result $result [, int $resulttype = MYSQLI_BOTH ]) 

mysqli_fetch_array需要mysqli_result,但使用的是mysql_query,你應該嘗試mysql_fetch_array

0

只要改變: -

while($row = mysqli_fetch_array($result)) 

while($row = mysql_fetch_array($result)) 
相關問題