I have 2 columns in a table called Points. The 2 columns are UserPoints and UserID.
我在一个名为Points的表中有2列。 2列是UserPoints和UserID。
I want to be able to echo the total amount of points a user has.
我希望能够回显用户拥有的总积分数。
I've got something like this but I dont think its right.
我有类似的东西,但我认为没错。
$getTotalPoints = mysql_query("SELECT SUM(UserPoints) FROM `Points` WHERE `UserID` = '1'") or die(mysql_error());
$totalPoints = mysql_fetch_array($getTotalPoints);
When i echo the above statement by echoing "$totalPoints" i get "Array".
当我通过回显“$ totalPoints”来回应上述陈述时,我得到“数组”。
Anyone know the correct query to do this ?
有人知道正确的查询吗?
4 个解决方案
#1
4
You're getting Array
because that's what's stored in $totalPoints
. Look closely at your code and you'll see you used the mysql_fetch_array()
function, which retrieves a row of results from the results set as an array. If you do var_dump()
on $totalPoints
you'll see the following:
你得到的数组是因为这是存储在$ totalPoints中的数据。仔细查看您的代码,您将看到使用了mysql_fetch_array()函数,该函数从结果集中检索一行结果作为数组。如果你在$ totalPoints上做var_dump(),你会看到以下内容:
Array
(
[0] => 12345
[SUM(UserPoints)] => 12345
)
The sum you're looking for is at index 0 or the column name, in this case SUM(UserPoints)
, so you can output it using echo $totalPoints[0]
or echo $totalPoints['SUM(UserPoints)']
.
您要查找的总和是索引0或列名称,在本例中为SUM(UserPoints),因此您可以使用echo $ totalPoints [0]或echo $ totalPoints ['SUM(UserPoints)']输出它。
Alternatively, you could use the mysql_result()
function. I think this is more in-line with the behavior you were expecting. It fetches a single value from the row from the result set. So, instead of mysql_fetch_array()
you'd wrote:
或者,您可以使用mysql_result()函数。我认为这更符合你期望的行为。它从结果集中的行中获取单个值。所以,你写的不是mysql_fetch_array(),而是写了:
$totalPoints = mysql_result($result, 0);
For more information on mysql_result()
, check out the PHP documentation for it.
有关mysql_result()的更多信息,请查看它的PHP文档。
As an aside, I would recommend not using mysql_* functions if you have the option. A newer interface like PDO, or at least mysqli, would be better. This will depend on your project of course... if you're working with a large legacy code base it may be difficult to change. But if you're starting out now, I think you'd benefit from the newer libraries. You can see my opinion and some guidance on transitioning extensions in this article I wrote.
顺便说一句,如果您有选项,我建议不要使用mysql_ *函数。像PDO这样的新界面,或者至少是mysqli,会更好。这当然取决于您的项目......如果您正在使用大型遗留代码库,则可能很难更改。但如果你现在开始,我想你会从新的图书馆中受益。您可以在我写的这篇文章中看到我对转换扩展的看法和一些指导。
Hope this helped... and good luck!
希望这有帮助......祝你好运!
#2
1
mysql_fetch_array fetches a result row as an associative array, a numeric array, or both. by default it creates both. all that you need is to echo $totalPoints[0];
mysql_fetch_array将结果行提取为关联数组,数字数组或两者。默认情况下它会同时创建。所有你需要的是回显$ totalPoints [0];
or, if you rewrite you request as
或者,如果你重写你的请求
$getTotalPoints = mysql_query("SELECT SUM(UserPoints) total FROM `Points`
WHERE `UserID` = '1'") or die(mysql_error());
$totalPoints = mysql_fetch_array($getTotalPoints);
echo $totalPoints['total'];
#3
1
mysql_fetch_array returns an array. Therefore you need to treat $totalpoints
as an array.
mysql_fetch_array返回一个数组。因此,您需要将$ totalpoints视为数组。
try adding this line to the end of your snippet:
尝试将此行添加到代码段的末尾:
echo $totalPoints[0];
echo $ totalPoints [0];
There are several ways to retrieve data with the mysql functions I suggest reading about them in the php manual.
有几种方法可以使用mysql函数检索数据,我建议在php手册中阅读它们。
Here is mysql_fetch_array
这是mysql_fetch_array
#4
1
The resultset row is an array with as many elements as you got in the SELECT. In you case you only got 1 element (the sum). So you should: echo $totalPoints[0];
If you need to debug this kind of issues I recommend you to read about print_r function.
结果集行是一个数组,其中包含与SELECT中相同的元素。在你的情况下,你只有1个元素(总和)。所以你应该:echo $ totalPoints [0];如果您需要调试此类问题,我建议您阅读有关print_r函数的信息。
#1
4
You're getting Array
because that's what's stored in $totalPoints
. Look closely at your code and you'll see you used the mysql_fetch_array()
function, which retrieves a row of results from the results set as an array. If you do var_dump()
on $totalPoints
you'll see the following:
你得到的数组是因为这是存储在$ totalPoints中的数据。仔细查看您的代码,您将看到使用了mysql_fetch_array()函数,该函数从结果集中检索一行结果作为数组。如果你在$ totalPoints上做var_dump(),你会看到以下内容:
Array
(
[0] => 12345
[SUM(UserPoints)] => 12345
)
The sum you're looking for is at index 0 or the column name, in this case SUM(UserPoints)
, so you can output it using echo $totalPoints[0]
or echo $totalPoints['SUM(UserPoints)']
.
您要查找的总和是索引0或列名称,在本例中为SUM(UserPoints),因此您可以使用echo $ totalPoints [0]或echo $ totalPoints ['SUM(UserPoints)']输出它。
Alternatively, you could use the mysql_result()
function. I think this is more in-line with the behavior you were expecting. It fetches a single value from the row from the result set. So, instead of mysql_fetch_array()
you'd wrote:
或者,您可以使用mysql_result()函数。我认为这更符合你期望的行为。它从结果集中的行中获取单个值。所以,你写的不是mysql_fetch_array(),而是写了:
$totalPoints = mysql_result($result, 0);
For more information on mysql_result()
, check out the PHP documentation for it.
有关mysql_result()的更多信息,请查看它的PHP文档。
As an aside, I would recommend not using mysql_* functions if you have the option. A newer interface like PDO, or at least mysqli, would be better. This will depend on your project of course... if you're working with a large legacy code base it may be difficult to change. But if you're starting out now, I think you'd benefit from the newer libraries. You can see my opinion and some guidance on transitioning extensions in this article I wrote.
顺便说一句,如果您有选项,我建议不要使用mysql_ *函数。像PDO这样的新界面,或者至少是mysqli,会更好。这当然取决于您的项目......如果您正在使用大型遗留代码库,则可能很难更改。但如果你现在开始,我想你会从新的图书馆中受益。您可以在我写的这篇文章中看到我对转换扩展的看法和一些指导。
Hope this helped... and good luck!
希望这有帮助......祝你好运!
#2
1
mysql_fetch_array fetches a result row as an associative array, a numeric array, or both. by default it creates both. all that you need is to echo $totalPoints[0];
mysql_fetch_array将结果行提取为关联数组,数字数组或两者。默认情况下它会同时创建。所有你需要的是回显$ totalPoints [0];
or, if you rewrite you request as
或者,如果你重写你的请求
$getTotalPoints = mysql_query("SELECT SUM(UserPoints) total FROM `Points`
WHERE `UserID` = '1'") or die(mysql_error());
$totalPoints = mysql_fetch_array($getTotalPoints);
echo $totalPoints['total'];
#3
1
mysql_fetch_array returns an array. Therefore you need to treat $totalpoints
as an array.
mysql_fetch_array返回一个数组。因此,您需要将$ totalpoints视为数组。
try adding this line to the end of your snippet:
尝试将此行添加到代码段的末尾:
echo $totalPoints[0];
echo $ totalPoints [0];
There are several ways to retrieve data with the mysql functions I suggest reading about them in the php manual.
有几种方法可以使用mysql函数检索数据,我建议在php手册中阅读它们。
Here is mysql_fetch_array
这是mysql_fetch_array
#4
1
The resultset row is an array with as many elements as you got in the SELECT. In you case you only got 1 element (the sum). So you should: echo $totalPoints[0];
If you need to debug this kind of issues I recommend you to read about print_r function.
结果集行是一个数组,其中包含与SELECT中相同的元素。在你的情况下,你只有1个元素(总和)。所以你应该:echo $ totalPoints [0];如果您需要调试此类问题,我建议您阅读有关print_r函数的信息。