我需要在 HTML 中显示涉及 COUNT(*) 的 MYSQL 查询
我需要使用 php 从 mysql 数据库获取查询结果。我四处寻找,我认为我所拥有的应该有效,但事实并非如此。我正在正确的文件夹中运行它。如果还有其他任何人可以告诉我,我将不胜感激。
谢谢!
这是我的代码。
<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>
I need to grab the result of a query, with php from a mysql database. I have searched around and I think what I have should work, but it doesn't. I'm running it in the correct folder. If there is anything else anyone can tell me, it would be greatly appreciated.
Thanks!
Here is my code.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一个错误:
事实上,在 while 之后和
{
之前有分号(它不应该在那里)。无论如何,由于您只检索一个字段,因此 while 根本没有用。摆脱它并执行如下操作:
You have an error here:
Infact you have the semicolon after the while and before the
{
(it should not be there).Anyway, since you're retrieving just one field, the while is useless at all. Get rid of it and do something like this: