2014-11-08 53 views
0

我想在我的PHP頁面上顯示MySQL表。 Table標題顯示正確,但沒有任何MySQL錶行出現,並且在MySQL錶行數據應該出現的地方也出現錯誤。當試圖在HTML表中顯示MySQL表數據時出現錯誤

Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in /home/www/s.com/yogaclub/index.php on line 61 

這是代碼到我的網頁:

<?php 
$username = "d"; 
$password = "dar"; 
$hostname = "localhost"; 

//connection to the database 
$con = mysqli_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL"); 

if ($con) { } else { echo "didn't work";} 
?> 

<!doctype html> 
<head> 
<style> 
body { 
    font-family:verdana; 
} 

#box { 
    background-color:yellow; 
    position:relative; 
    color:red; 
    padding-top:10px; 
    margin:auto; 
    width:600px; 
    border-radius:15px 
    padding-bottom:10px;  
} 

    .container { 
     width: 500px; 
     clear: both; 
    } 
    .container input { 
     width: 100%; 
     clear: both; 
    } 


#pic { 
    align:left 
} 
</style>  
<title>Yoga Club</title> 
</head> 
<body bgcolor="blue"> 
<div id="box"> 
<h3><center>Yoga Club!</center></h3> 
<img src="thOX7T2TIR.jpg" id="pic"><span style="text-align:center; align:right"><span style="color:blue;"></span> 
<?php 

$result = mysqli_query($con, "SELECT * FROM yogaclub"); 

echo "<table border='1'> 
<tr> 
<th>First Name</th> 
<th>Last Name</th> 
</tr>"; 

while($row = mysqli_fetch_row($result)) 
{ 
echo "<tr>"; 
echo "<td>" . $row['fname'] . "</td>"; 
echo "<td>" . $row['lname'] . "</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 

mysqli_close($con); 
?><br /><br /><center>Please Sign up to the Adventures of Yoga. 
We will be doing lots of different kinds of stretches.</center> 
<?php /* if(isset($_POST['add'])) 
{ 
$sql = mysql_query("insert into yogaclub(fname, lname) values('$_GET['fname']', '$_GET['lname']')"); 
} else { echo "nonsend"; } */ 
?><br /><form method="post" action="<?php $_PHP_SELF ?>">First Name: <input name="fname" type="text"><Br />Last Name: <input type="text"><br />Address: <input type="text"><br />Email: <input type="text"><br />Phone Number: <input type="text"><input type="hidden" value="send" name="send"><br />Yoga Experience: <select> 
    <option value="none">None</option> 
    <option value="alittle">A little</option> 
    <option value="alot">A lot</option> 
</select> <br />Age: <input type="text" size="3"><br /><br /> 
<input type="submit" name="add" value="Sign me up!"></form></span> 
</div> 
</body> 
</html> 

感謝您的幫助,當我運行我的PHP頁面,我得到這個錯誤。所有的幫助非常感謝。

+1

來測試你實際上連接到數據庫? – 2014-11-08 04:13:59

+0

意味着您的查詢不起作用! – Rizier123 2014-11-08 04:15:24

+0

@ Fred-ii-不認爲他忘記了連接。因爲他有錯誤,但沒有得到'注意:未定義的變量:con'我認爲他有一個連接 – Rizier123 2014-11-08 04:16:53

回答

0

而不是

while($row = mysqli_fetch_row($result)) 

請儘量使用

while($row= mysqli_fetch_assoc($result)) 
0

測試$ result以驗證查詢是否返回至少一行可能是一個好主意。

+0

如何測試$結果? – Kelsey 2014-11-08 04:24:14

+0

http://php.net/manual/en/mysqli-result.num-rows.php mysqli_num_rows() – 2014-11-08 05:28:56

相關問題