简体   繁体   English

输出子查询

[英]Outputting a sub-query

<!DOCTYPE html>
<html>
<?php
// Connecting to SQL server
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "super_athletics";

// Creates connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
} else {
    echo "Connected!";
}
echo "<br>";

// Sums upp all the points recieved by each student for specific school
$myQuery = "SELECT SUM(result_studpoints) AS total FROM result WHERE stud_id IN (SELECT stud_id FROM students WHERE stud_school = 'CCA')";
$result = mysqli_query($myQuery);

mysqli_close($conn);
?>
</html>

The problem I am having with the code above is the fact that I cannot output $myQuery. 我上面的代码有问题是我无法输出$ myQuery。 This is a sub-query that works out the sum of points for a specific school. 这是一个子查询,可以计算出特定学校的分数总和。 The sub-query selects all the student id's who go to a specific school from the student table, in this case 'CCA', and uses the id's to sum up the points (which is gained from result table) received, which should give the total of points for the specific school (CCA). 子查询从学生表中选择去某所学校的所有学生ID(在本例中为“ CCA”),并使用该ID汇总接收到的分数(从结果表中获得),从而得出特定学校(CCA)的总分。

The subquery works well on sql alone but I cannot get it to output with php. 子查询仅在sql上运行良好,但无法用php输出。 I have tried echoing but nothing comes up. 我试过了回声,但没有任何反应。 This may seem simple but I have not found a solution, please help! 这看似简单,但我尚未找到解决方案,请帮忙!

Either the tutorial you followed is woefully incomplete. 您所遵循的教程都不完整。 Or you skipped over a bunch of stuff because you thought it wasn't necessary. 或者您跳过了一堆东西,因为您认为这不是必需的。

One problem in the code is the call to the mysqli_query function. 代码中的一个问题是对mysqli_query函数的调用。 The first argument to that function should be the connection: $conn . 该函数的第一个参数应该是连接: $conn

$result = mysqli_query($conn,"SELECT ... ");
                       ^^^^^^

mysqli_query should evaluate to FALSE when an error has occurred. 发生错误时, mysqli_query应评估为FALSE。 Otherwise, it will evaluate to TRUE. 否则,它将评估为TRUE。 The code should perform some kind of check, and handle an error. 该代码应执行某种检查,并处理错误。 This isn't necessarily the best way to code this: 这不一定是编写此代码的最佳方法:

  if(!$result) {
    die("query execution failed");
  }

Once the code has determined that $result is a handle to a valid resultset, the code can then "fetch" the results. 一旦代码确定$result是有效结果集的句柄,则代码可以“获取”结果。 You might want to revisit the tutorial, focusing on the part about retrieving the rows from the resultset. 您可能需要重新访问本教程,重点是有关从结果集中检索行的部分。 Processing of the rows is usually performed in a loop. 行的处理通常在循环中执行。

You were missing the connection in mysqli_query and you didn't fetch the data 您丢失了mysqli_query中的连接,并且未获取数据

// Sums upp all the points recieved by each student for specific school
$myQuery = "SELECT SUM(result_studpoints) AS total FROM result WHERE stud_id IN (SELECT stud_id FROM students WHERE stud_school = 'CCA')";
$result = mysqli_query( $conn, $myQuery);
$data = mysqli_fetch_assoc( $result );
echo $data[ 'total' ];
mysqli_close($conn);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM