2011-11-04 70 views
0

我需要從mysql數據庫中獲取查詢的結果。我已經四處搜尋,我認爲我應該工作,但事實並非如此。我在正確的文件夾中運行它。如果有什麼人可以告訴我,這將不勝感激。我需要在HTML中顯示一個MYSQL查詢COUNT(*)

謝謝!

這是我的代碼。

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
</head> 
<body> 
    <?php 
     // define DB connection variables 
     $host = "localhost"; 
     $user = "root"; 
     $password = ""; 
     // connect to the MySQL server 
     $db_connection = mysql_connect($host,$user,$password); 

     // If the connection failed: 
     if(!$db_connection){ 
      echo "Error connecting to the MySQL server: " . mysql_error(); 
      exit; 
     } 
     mysql_select_db("hockey"); 

     // Execute an SQL SELECT query to pull back rows where Kessel scores 
     $query = "SELECT COUNT(*) FROM goals WHERE player_id = 4"; 
     $response = mysql_query($query) or die(mysql_error()); 
    ?> 

    <?php 
     while($row= mysql_fetch_array($response));{ 

     echo $row['COUNT(*)']; 

     // get the next row's details and loop to the top 

     } 
    ?> 
</body> 
</html> 
+0

您是否收到任何錯誤? –

+0

究竟是如何不工作? –

回答

3

您這裏有一個錯誤:你有一段時間後,分號和{之前(它不應該存在

while($row= mysql_fetch_array($response));{ 

逸岸。

無論如何,由於您只需要檢索一個字段,所以一切都毫無用處。擺脫它,並做這樣的事情:

$query = "SELECT COUNT(*) as Count FROM goals WHERE player_id = 4"; 
$response = mysql_query($query) or die(mysql_error()); 
$row= mysql_fetch_array($response); 
echo $row['Count']; 
+0

感謝您的幫助。它解決了我的問題。我感謝幫助! –