PHP MySQL查询没有返回完整的所需结果集

时间:2011-09-17 19:43:27

标签: php mysql sql

我在PHP变量中放置了以下mysql_query

$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";

我使用以下PHP将结果放入一个关联数组中

$equalDimensions_data = mysql_query($equalDimensions_query) or die('MySql Error' . mysql_error());

while ($row = mysql_fetch_assoc($equalDimensions_data)) { 
    $cnt[$row['COL1']] = $row['imgCount']; 
}

假设返回一组三个数组,其中第一个为allEqual,第二个为widthEqual,第三个为heightEqual(顺序无关紧要) )。

唉,由于某种原因 还没有<{1}}:

allEqual

当我使用Array ( [heightEqual] => 0 [widthEqual] => 0 ) 以原始的“三个数组”形式显示检索到的数据时,我得到的结果只有两个数组:

print_r

然而,如果我使用Array ( [COL1] => heightEqual [imgCount] => 0 ) Array ( [COL1] => widthEqual [imgCount] => 0 ) 而没有循环,如下所示:

print_r

我返回了之前缺少的$equalDimensions_data = mysql_query($equalDimensions_query) or die('MySql Error' . mysql_error()); $equalDimensions_array = mysql_fetch_assoc($equalDimensions_data); print("<pre>"); print_r($equalDimensions_array); print("</pre>"); 数组:

allEqual

我理解由于在最后一种情况下缺少Array ( [COL1] => allEqual [imgCount] => 2 ) 循环,我只返回一个结果;但是为什么呢,在while循环的情况下,while结果似乎被跳过了?这是我的代码的问题吗?我感谢您提供的任何帮助。我为这么长的问题道歉;我想确保尽可能多地提供信息。


您可以在此处下载我的数据库架构:https://files.me.com/stefanmelnychenko/453l4z

1 个答案:

答案 0 :(得分:1)

这是您复制的地方。您不会更改变量或任何内容以复制任何错误(如果有)。 sql echoed

我检查了你的原理,一切都很好,这是php脚本:

<?PHP

// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());

//select database   
mysql_select_db("new_arrivals_imgs") or die(mysql_error());


$imgId=1;
$maxImageHeight=1;
$maxImageWidth=1;

 $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']; 
}

print_r($cnt);

?>

这就是结果:

enter image description here