繁体   English   中英

我需要使用COUNT(*)来显示HTML中的MYSQL查询

[英]I need to display a MYSQL query in HTML in volving COUNT(*)

我需要从mysql数据库中获取php的查询结果。 我四处搜寻,我认为自己应该有的工作,但是没有。 我正在正确的文件夹中运行它。 如果还有人可以告诉我的话,将不胜感激。

谢谢!

这是我的代码。

<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>

您在这里有一个错误:

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'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM