PHP MySQL计数查询结果为字符串,而不是整数 [英] PHP MySQL Count Query Result is String, Not Integer
问题描述
我有以下MySQL查询,以及用于将Count
结果格式化为单个数组的PHP代码:
I have the following MySQL query, and PHP code to format the Count
result in a single array:
$equalDimensions_query =
"SELECT 'allEqual' AS COL1,COUNT(*) AS imgCount FROM (
SELECT imgHeight, imgWidth, imgId AS primaryId FROM primary_images
UNION ALL
SELECT imgHeight, imgWidth, primaryId FROM secondary_images
) AS union_table
WHERE primaryId = $imgId AND imgWidth = $maxImageWidth AND imgHeight = $maxImageHeight
UNION ALL
SELECT 'widthEqual' AS COL1,COUNT(*) AS imgCount FROM (
SELECT imgHeight, imgWidth, imgId AS primaryId FROM primary_images
UNION ALL
SELECT imgHeight, imgWidth, primaryId FROM secondary_images
) AS union_table
WHERE primaryId = $imgId AND imgWidth = $maxImageWidth AND imgHeight != $maxImageHeight
UNION ALL
SELECT 'heightEqual' AS COL1,COUNT(*) AS imgCount FROM (
SELECT imgHeight, imgWidth, imgId AS primaryId FROM primary_images
UNION ALL
SELECT imgHeight, imgWidth, primaryId FROM secondary_images
) AS union_table
WHERE primaryId = $imgId AND imgWidth != $maxImageWidth AND imgHeight = $maxImageHeight";
$equalDimensions_data = mysql_query($equalDimensions_query) or die('MySql Error' . mysql_error());
while ($row = mysql_fetch_assoc($equalDimensions_data)) {
$cnt[$row['COL1']] = $row['imgCount'];
}
var_dump($cnt);
(注意,我将var_dump($cnt)
作为最后一行包括在内)
(Notice I included var_dump($cnt)
as the last line)
var_dump
返回了以下数组:
The var_dump
returned the following array:
array
(
'allEqual' => string '2' (length=1)
'widthEqual' => string '0' (length=1)
'heightEqual' => string '0' (length=1)
)
我很好奇为什么计数是作为字符串而不是整数返回的?
是否应将其返回为整数?还是处理计数查询时将结果作为字符串返回是正常的做法?
Should it be returned as an integer? Or is it the norm when dealing with count queries, for the result to be returned as a string?
谢谢.
推荐答案
来自
返回与字符串对应的关联字符串数组
获取的行,如果没有更多行,则返回FALSE.
Returns an associative array of strings that corresponds to the
fetched row, or FALSE if there are no more rows. (强调我的意思),对于MySQL驱动程序来说,将所有内容作为字符串提供给您,并假设您知道如何使用它,可能会更容易些.只需使用 intval ()即可获取整数值. (emphasis mine) It's probably just easier for the MySQL driver to give you everything as a string, and assume you will know what to do with it. Just use intval() to get the integer value. 这篇关于PHP MySQL计数查询结果为字符串,而不是整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!