没有总结整列

时间:2014-07-31 03:31:39

标签: php mysql sql

因为它没有计算SUM first_payment的整个列。 I tried "SELECT SUM(first_payment) FROM \客户信息``,它作为未识别的索引first_payment返回。有什么理由吗?

    <?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);


    $pay=$_REQUEST['payment']; 

    // MySQL database connection, username, password, database name
    $con=mysqli_connect("localhost","user","pass","db_name");

    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    if ($pay=="first") 
      $query="SELECT *, SUM(first_payment) FROM `Customer Information`";
else if ($pay=="second") 
      $query="SELECT *, SUM(second_payment) FROM `Customer Information`";
else if ($pay=="third") 
      $query="SELECT *, SUM(third_payment) FROM `Customer Information`";
else if ($pay=="fourth") 
      $query="SELECT *, SUM(fourth_payment) FROM `Customer Information`";


    $result = mysqli_query($con,$query);

    // Executing and error checking of query
    if (!mysqli_query($con,$query)) {
      die('Error: ' . mysqli_error($con));
    }


    while ($row = mysqli_fetch_array($result)) {
    switch($pay)
    {
       case "first":
          echo $row['first_payment'];
          break;
       case "second":
          echo $row['second_payment'];
          break;
       case "third":
          echo $row['third_payment'];
          break;
       case "fourth":
          echo $row['fourth_payment'];
          break;
       default:
          echo "nothing selected";
    }
    }
    mysqli_close($con);
    ?>

3 个答案:

答案 0 :(得分:3)

更改您的查询并为您在代码中使用的总金额提供正确的别名。试试这个

if ($pay=="first") 
   $query="SELECT *, SUM(first_payment) AS first_payment FROM `Customer Information`";
else if ($pay=="second") 
   $query="SELECT *, SUM(second_payment) AS second_payment FROM `Customer Information`";
else if ($pay=="third") 
   $query="SELECT *, SUM(third_payment) AS third_payment FROM `Customer Information`";
else if ($pay=="fourth") 
   $query="SELECT *, SUM(fourth_payment) AS fourth_payment FROM `Customer Information`";

答案 1 :(得分:2)

  1. 从查询中提取数组时使用MYSQLI_ASSOC:

    mysqli_fetch_array($result,MYSQLI_ASSOC);

    或使用mysqli_fetch_assoc()函数:

    mysqli_fetch_assoc($result)

  2. 在查询中为摘要列使用别名:

  3. 例如:

    "SELECT *, SUM(first_payment) AS SUMMARY  FROM `Customer Information`";
    

    此外,最好通过在任何操作之前检查$pay变量来避免与数据库的不必要连接,如下所示:

    <?php
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
    
    $pay=$_REQUEST['payment']; 
    // first of all: let's check $pay variable:
    switch($pay)
    {
       case "first":
          $row_index = 'first_payment';
          break;
       case "second":
          $row_index = 'second_payment';
          break;
       case "third":
          $row_index = 'third_payment';
          break;
       case "fourth":
          $row_index = 'fourth_payment';
          break;
       default:
          echo "nothing selected";
          return;  // return if nothing is selected, no need to run any database manipulations.
    }
    
    // MySQL database connection, username, password, database name
    $con=mysqli_connect("localhost","user","pass","db_name");
    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    // prepare query:
    $query="SELECT *, SUM(".$row_index.") AS SUMMARY  FROM `Customer Information`";
    
    // Executing and error checking of query
    if (!$result = mysqli_query($con,$query)) {
      die('Error: ' . mysqli_error($con));
    }
    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
      echo $row['SUMMARY'];
    }
    mysqli_free_result($result); // don't forget to free query results
    mysqli_close($con);
    ?>
    

答案 2 :(得分:1)

除了上面提供的答案,你必须知道出了什么问题:

在您的select语句中,您选择了字段的总和,而不是您尝试检索其结果的字段本身,因此错误未定义索引...

如上面的答案,给总和一个别名,并在while循环中使用它来检索它。