【发布时间】:2014-01-26 17:50:48
【问题描述】:
我正在计算有多少胜/负以及玩了多少场比赛。
我有一个名为“周”的数据库,其中存储了很多列,但现在需要计算所有周的输赢总数,我搜索并找到了 this page,它显示了一个很好的示例,说明如何使用 SUM。
我的表被命名为周,它看起来像这样
id | fs1 | fs2 | fs3 | => fs12
例如,我每周都在插入行
id | fs1 | fs2 | fs3 | => fs12
1 0 1 0 1 =2 wins
2 0 1 1 1 =3 wins
所以使用我上面链接到的网站的代码;这是我目前拥有的代码,但它不起作用。
<?
//set connection variables
$host = "localhost";
$username = "root";
$password = "";
$db_name = "pool"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
echo "Error: Could not connect to database.";
exit;
}
$getTotalPoints = mysql_query("SELECT SUM(fs1 + fs2 + fs3 + fs4 + fs5 + fs6 + fs7 + fs8 + fs9 + fs10 + fs11 + fs12) wins FROM weeks");
$TotalPoints = mysql_fetch_array($getTotalPoints);
echo $TotalPoints['wins'];
?>
这段代码给了我以下错误Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in line 6
$TotalPoints = mysql_fetch_array($getTotalPoints);
当我直接对数据库进行查询时,我收到以下消息
This table does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available.
Showing rows 0 - 0 ( 1 total, Query took 0.0012 sec)
wins
59
【问题讨论】:
-
首先,测试你的查询,你的查询可能有错误或你的
'db_connect.php'可能有问题 -
所以?问题出在你的
'db_connect.php',必须有mysql_connect()和mysql_select_db()方法。有这些方法吗? -
啊!您正在使用
mysqli进行连接!所以你不能使用mysql_query -
只需将
mysql_更改为mysqli_。