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 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配