2012-11-27 35 views
0
<?php 

$con = mysql_connect("localhost","root",""); 

if (!$con) 


{ 

die('Could Not Connect: '. mysql_error()); 

} 
// This block grabs the whole list for viewing 

$bilboard_list = ""; 
$bilboard_list0 = ""; 
$bilboard_list1 = ""; 
$bilboard_list2 = ""; 
$bilboard_list3 = ""; 
$bilboard_list4 = ""; 
$bilboard_list5 = ""; 
$bilboard_list6 = ""; 
$bilboard_list7 = ""; 
$bilboard_list8 = ""; 
$bilboard_list9 = ""; 
$bilboard_list10 = ""; 
$bilboard_list11 = ""; 
$bilboard_list12 = ""; 

$sql = mysql_query("SELECT * FROM maklumat_peribadi ORDER BY id"); 

$bilboardCount = mysql_num_rows($sql); // count the output amount 

if ($bilboardCount > 0) { 
while($row = mysql_fetch_array($sql)){ 


$id = $row["id"]; 
$nama_penuh = $row["nama_penuh"]; 
$no_ic = $row["no_ic"]; 
$alamat_ttp = $row["alamat_ttp"]; 
$no_rumah = $row["no_rumah"]; 
$no_bimbit = $row["no_bimbit"]; 
$jantina = $row["jantina"]; 
$tmpt_lhir = $row["tmpt_lhir"]; 
$email = $row["email"]; 
$agama = $row["agama"]; 
$bangsa = $row["bangsa"]; 
$negara = $row["negara"]; 
$nama_penjaga = $row["nama_penjaga"]; 
$nama_ibu = $row["nama_ibu"]; 



//for show 

$bilboard_list .= "$No<br>"; 
$bilboard_list0 .= "$Nama<br>"; 
$bilboard_list1 .= "$No Kad Pengenalan<br>"; 
$bilboard_list2 .= "$Alamat<br>"; 
$bilboard_list3 .= "$No Telefon Rumah<br>"; 
$bilboard_list4 .= "$No Telefon Bimbit<br>"; 
$bilboard_list5 .= "$Jantina<br>"; 
$bilboard_list6 .= "$Tempat Lahir<br>"; 
$bilboard_list7 .= "$Email<br>"; 
$bilboard_list8 .= "$Agama<br>"; 
$bilboard_list9 .= "$Bangsa<br>"; 
$bilboard_list10 .= "$Warganegara<br>"; 
$bilboard_list11.= "$Nama Penjaga/Bapa<br>"; 
$bilboard_list12 .= "$Nama Ibu<br>"; 

} 
} 
?> 

<html> 
<head> 
<link rel="stylesheet" href="2col_leftNav.css" type="text/css" /> 
<style type="text/css"> 
<!-- 
.style1 { 
    font-size: 12px; 
    font-weight: bold; 
} 
--> 
</style> 
</head> 

<body> 
<div id="masthead"> 
    <h1 id="siteName">Admin Site</h1> 
    <div id="globalNav"> <a href="adminpage.php"> Halaman Utama</a> | <a href="test.php">Maklumat Peribadi Pelajar | <a href="adminpage.php">Senarai Pelajar Mengikut Kursus | <a href="logout.php">Logout</a></div> 
</div> 
<table width="1000px" border="1" align="center"> 

     <tr> 

     <td width="auto"><span class="style1"><strong>NO<strong></span></td> 
     <td width="auto"><span class="style1"><strong>NAMA</strong></td> 
     <td width="auto"><span class="style1"><strong>NO KAD PENGENALAN</strong></td> 
     <td width="auto"><span class="style1"><strong>ALAMAT</strong></td> 
     <td width="auto"><span class="style1"><strong>NO TELEFON RUMAH</strong></td>\ 
     <td width="auto"><span class="style1"><strong>NO TELEFON BIMBIT</strong></td> 
     <td width="auto"><span class="style1"><strong>JANTINA</strong></td> 
     <td width="auto"><span class="style1"><strong>TEMPAT LAHIR</strong></td> 
     <td width="auto"><span class="style1"><strong>EMAIL</strong></td>\ 
     <td width="auto"><span class="style1"><strong>AGAMA</strong></td> 
     <td width="auto"><span class="style1"><strong>BANGSA</strong></td> 
     <td width="auto"><span class="style1"><strong>WARGANEGARA</strong></td> 
     <td width="auto"><span class="style1"><strong>NAMA PENJAGA/BAPA</strong></td> 
     <td width="auto"><span class="style1"><strong>NAMA IBU</strong></td> 



     </tr> 

     <tr> 

     <td><?php echo $bilboard_list; ?></td> 
     <td><?php echo $bilboard_list0; ?></td> 
     <td><?php echo $bilboard_list1; ?></td> 
     <td><?php echo $bilboard_list2; ?></td> 
     <td><?php echo $bilboard_list3 ?></td> 
     <td><?php echo $bilboard_list4; ?></td> 
     <td><?php echo $bilboard_list5; ?></td> 
     <td><?php echo $bilboard_list6; ?></td> 
     <td><?php echo $bilboard_list7; ?></td> 
     <td><?php echo $bilboard_list8; ?></td> 
     <td><?php echo $bilboard_list9; ?></td> 
     <td><?php echo $bilboard_list10; ?></td> 
     <td><?php echo $bilboard_list11; ?></td> 
     <td><?php echo $bilboard_list12; ?></td> 



     </tr> 

</table> 

<body> 

</html> 
` 

我有這段代碼和我在一起。 但每當我嘗試運行這段代碼會顯示此:表有問題

警告:mysql_num_rows()預計參數1是資源,布爾在d給出:\ XAMPP \ htdocs中\ ftn_eg \上線32 table.php

任何人都可以幫助我嗎?

+1

'mysql_'函數不安全並且不推薦使用。改用mysqli或PDO。我推薦PDO http://php.net/manual/en/book.pdo.php – Raekye

+0

您的查詢失敗。你需要檢查失敗,如'if(!$ sql)echo mysql_error();' –

+0

作爲一個附註,你應該改變'$ bilboard_list','$ bilboard_list0' ...'$ bilboard_list12'到一個數組。 – jeff

回答

0

您的查詢

SELECT * FROM maklumat_peribadi ORDER BY id 

是返回false。檢查數據庫

+0

**編輯** 改用mysqli庫 - mysql已棄用。 –

1

在你的代碼所缺少選擇的數據庫功能

您運行mysql_query功能

+0

或者使用$ con作爲查詢參數執行 –

+0

您可以通過使用'mysql_error'函數將錯誤與$ sql = mysql_query(「SELECT * FROM maklumat_peribadi ORDER BY id」)或die(mysql_error())一起使用。 '我認爲在這種情況下,它會返回「沒有選擇數據庫」作爲錯誤。 –

+0

我說'$ con'作爲參數在查詢執行,如'mysql_query('SQL QUERY',$ con)' –

0

讓之前應該使用mysql_select_db('your_db_name');,先說開始,不要使用mysql_ *函數。這一次已過,您現在應該使用mysqliPDO

這樣說,你沒有選擇一個數據庫。 MySQL應該從哪裏撤出?

mysql_select_db('DATABASE_NAME');

接下來,會嘗試打印此信息時,很多可預見的語法錯誤。 $bilboard_list3 .= "$No Telefon Rumah<br>";這應該是我能讀的變量,需要像$bilboard_list3 .= "$No_Telefon_Rumah<br>";

最後,這些變量中的大多數可以減少到一個數組。

0

您需要使用mysql_select_db來選擇模式。您只能連接到數據庫系統,而不能連接到特定的數據庫。

這是一個很好的習慣,您使用if ($bilboardCount > 0) {檢查結果,但是您的輸出結果並不是看是否有結果顯示。您可能會在您的網頁上看到錯誤訊息。

使用PDO會使一些事情變得簡單和安全。即使數據來自數據庫,當您在頁面上顯示數據時,也應該使用htmlspecialchars進行轉義。

改善的好事是創建一個對象$ bilboard_list :)

繼續努力吧! :)