2011-11-04 67 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

感谢您的帮助。它解决了我的问题。我感谢帮助! –