2013-11-24 310 views
1

我正在學習用lynda.com.I的視頻PHP創建了一個在我的本地主機的phpmyadmin面板稱爲widget_corp數據庫,我寫這些代碼塊PHP,MySQL數據庫連接

<?php 
/* 1.Create a database connection */ 
$connection = mysql_connect("localhost", "root", "*"); 
if(!$connection){ 
die("Database connection failed: " .mysql_error()); 
} 

/* 2. Select a database to use */ 
$db_select = mysql_select_db("widget_corp", $connection); 
if(!$db_select) 
{ 
die("Database selection failed: " . mysql_error()); 
} 
?> 

    <html> 
    <head> 
    <title> Connection To the Database </title> 
    </head> 
    <body> 
    <?php 

//3. Perform database query 
$result = mysql_query("SELECT * FROM subjects",$connection); 
if(!$result) 
{ 
die("Database query failed: " .mysql_error()); 
} 

//4. Use returned data 

while($row = mysql_fetch_array($result)); 
{ 
echo $row["menu_name"]." ".$row["position"]."<br/>"; 
} 
?> 
</body> 
</html> 
<?php 
//5. Close connection 
mysql_close($connection); 
?> 

我總是得到這樣的錯誤類型:

Object not found! 

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again. 

If you think this is a server error, please contact the webmaster. 

Error 404 

localhost 
Apache/2.4.4 (Unix) PHP/5.5.3 OpenSSL/1.0.1e mod_perl/2.0.8-dev Perl/v5.16.3 

我該如何解決這個問題?謝謝所有

+0

請告訴我文件的名稱和最新的URLü去? – Rob

+0

file conn.php url:http://localhost/xampp/conn.php – user3026894

+1

它必須與您的文件的位置。代碼看起來不錯。嘗試將代碼直接放在www根目錄中,直接瀏覽到http://localhost/conn.php – Rob

回答

5

你犯了一個很小的錯誤。 while語句中使用了分號,所以只需將其刪除即可。然後它會正常工作。

使用返回的數據:

while($row = mysql_fetch_array($result)) 

不:

while($row = mysql_fetch_array($result));