将值等于ID的所有行相加

时间:2022-11-06 13:14:02

I've been looking up around for a couple tutorials of this and I've seemed out of luck. Basically, I have a database containing a winner's user ID (corresponding to the winners user ID) and a loser's ID. I am trying to create a members profile where it counts up all the rows the member has won. Here is what I have came up with:

我一直在找一些关于这个的教程,我似乎运气不太好。基本上,我有一个包含赢家用户ID(对应于赢家用户ID)和输家用户ID的数据库。以下是我的想法:

$web = mysqli_query("select SUM(matches) WHERE WinnerUID='".$req_user_info['id']."'"); 
$web_sum=mysqli_fetch_assoc($web); 
echo $web_sum;

Unfortunately, it doesn't display any number. Can anyone help?

不幸的是,它没有显示任何数字。谁能帮忙吗?

1 个解决方案

#1


3  

I think you're looking for COUNT() not SUM(). And you didn't include a table name. Also remember that mysqli_fetch_assoc() returns the row as an array, it doesn't return the first column's value. Also, mysqli_query() requires the connection as the first argument.

我想你是在找COUNT()而不是SUM()你没有包括表名。还要记住,mysqli_fetch_assoc()将行作为数组返回,它不会返回第一列的值。而且,mysqli_query()需要将连接作为第一个参数。

$web = mysqli_query($conn, "select COUNT(*) as total FROM matches WHERE WinnerUID='".(int)$req_user_info['id']."'"); 

$row = mysqli_fetch_assoc($web); 
echo $row['total'];

Don't concatenate variables into your SQL. Use a Prepared Statement with bound parameters. I have casted your ID as an (int) in the above code, which is a quick fix but you should switch to a Prepared Statement.

不要将变量连接到SQL中。使用带绑定参数的准备语句。在上面的代码中,我将您的ID设为(int),这是一个快速修复,但是您应该切换到准备好的语句。

Prepared Statement example (object oriented interface instead of procedural):

准备好的语句示例(面向对象的接口,而不是过程):

if ($stmt = $conn->prepare("select COUNT(*) from matches WHERE WinnerUID = ?")) {

    $stmt->bind_param("i", $req_user_info['id']);
    $stmt->execute();
    $stmt->bind_result($web_sum);
    $stmt->fetch();

    echo $web_sum;

    $stmt->close();
}

#1


3  

I think you're looking for COUNT() not SUM(). And you didn't include a table name. Also remember that mysqli_fetch_assoc() returns the row as an array, it doesn't return the first column's value. Also, mysqli_query() requires the connection as the first argument.

我想你是在找COUNT()而不是SUM()你没有包括表名。还要记住,mysqli_fetch_assoc()将行作为数组返回,它不会返回第一列的值。而且,mysqli_query()需要将连接作为第一个参数。

$web = mysqli_query($conn, "select COUNT(*) as total FROM matches WHERE WinnerUID='".(int)$req_user_info['id']."'"); 

$row = mysqli_fetch_assoc($web); 
echo $row['total'];

Don't concatenate variables into your SQL. Use a Prepared Statement with bound parameters. I have casted your ID as an (int) in the above code, which is a quick fix but you should switch to a Prepared Statement.

不要将变量连接到SQL中。使用带绑定参数的准备语句。在上面的代码中,我将您的ID设为(int),这是一个快速修复,但是您应该切换到准备好的语句。

Prepared Statement example (object oriented interface instead of procedural):

准备好的语句示例(面向对象的接口,而不是过程):

if ($stmt = $conn->prepare("select COUNT(*) from matches WHERE WinnerUID = ?")) {

    $stmt->bind_param("i", $req_user_info['id']);
    $stmt->execute();
    $stmt->bind_result($web_sum);
    $stmt->fetch();

    echo $web_sum;

    $stmt->close();
}