我有以下MySQL查询和PHP代码,用于将Count
结果格式化为单个数组:
$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)
列为最后一行)
var_dump
返回了以下数组:
array
(
'allEqual' => string '2' (length=1)
'widthEqual' => string '0' (length=1)
'heightEqual' => string '0' (length=1)
)
我很好奇为什么计数作为字符串返回,而不是整数?
它应该作为整数返回吗?或者它是处理计数查询时的标准,结果是作为字符串返回?
感谢。
答案 0 :(得分:7)
来自http://www.php.net/manual/en/function.mysql-fetch-assoc.php:
返回对应于的关联字符串数组 获取行,如果没有更多行,则返回FALSE。
(强调我的)MySQL驱动程序可能只是更容易为您提供一切字符串,并假设您将知道如何处理它。只需使用intval()来获取整数值。
答案 1 :(得分:1)
所有内容都以字符串¹的形式返回,无论它是否为列值(列的类型不起任何作用)或函数的结果(返回值的类型也不起作用)。
¹谈到MySql驱动程序时,至少。