我正在计算有多少胜/负以及多少场比赛。
我有一个名为周的数据库,在那里我存储了不少列,但现在需要计算我搜索过的所有周的获胜和总损失总数,并找到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
答案 0 :(得分:1)
您已为数据库连接创建了mysqli
对象,因此您必须使用至少需要2个参数的mysqli_query
方法。
$getTotalPoints = mysqli_query($mysqli,
"SELECT SUM(fs1 + fs2 + fs3 + fs4 + fs5 + fs6 +
fs7 + fs8 + fs9 + fs10 + fs11 + fs12) wins FROM weeks");
此外,对于mysql_fetch_array()
方法,您必须使用mysqli_
一个
$TotalPoints = mysqli_fetch_array($getTotalPoints);