donglefu6195 2014-01-26 17:50
浏览 39
已采纳

使用错误布尔值计算多列中的值

I am trying to calculate how many wins/ loses and how many games played.

I have a database named weeks and in there i store quite a few columns but now needing to calculate the total number of wins and loses for all weeks i have searched around and found this page which shows a nice example on how to use SUM.

My table is named weeks and it looks like this

id | fs1 | fs2 | fs3 | => fs12

I have been inserting rows for each week so for example

id | fs1 | fs2 | fs3 | => fs12
1     0     1     0        1           =2 wins
2     0     1     1        1           =3 wins

So using the code from the website i linked to above; this is my code i have at present but its not working.

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

This code gives me the following error Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in line 6

Which is $TotalPoints = mysql_fetch_array($getTotalPoints);

When i do the query direct to the database i get the following messages

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
  • 写回答

1条回答 默认 最新

  • 普通网友 2014-01-26 18:30
    关注

    You have created mysqli object for database connection, so you have to use mysqli_query method which need at least 2 parameters.

    $getTotalPoints = mysqli_query($mysqli, 
    "SELECT SUM(fs1 + fs2 + fs3 + fs4 + fs5 + fs6 + 
    fs7 + fs8 + fs9 + fs10 + fs11 + fs12) wins FROM weeks");
    

    Also, for the mysql_fetch_array() method you have to use mysqli_ one

    $TotalPoints = mysqli_fetch_array($getTotalPoints);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上