duankeng9477 2011-09-17 19:43
浏览 66
已采纳

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

I have the following mysql_query which I have placed in a PHP variable:

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

I am using the following PHP to place the results into one single associative array:

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

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

It is suppose to return a set of three arrays with the values of allEqual in the first, widthEqual in the second, and heightEqual in the third (the order does not matter).

Alas for some reason it is not returning allEqual:

Array
(
    [heightEqual] => 0
    [widthEqual] => 0
)

When I use print_r to display the retrieved data in its original 'three array' form, I get the same result of only two arrays:

Array
(
    [COL1] => heightEqual
    [imgCount] => 0
)
Array
(
    [COL1] => widthEqual
    [imgCount] => 0
)

Yet, if I use print_r without the loop as follows:

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

I am returned the previously missing allEqual array:

Array
(
    [COL1] => allEqual
    [imgCount] => 2
)

I understand that because of the absence of the while loop in the last case, I am only being returned one result; But why is it, that in the case of the while loop, the allEqual result appears to be skipped over? Is it a problem with my code? I appreciate any help you can provide. And I apologize for such a long question; I wanted to be sure I provided as much information as I could.


You can download my database schema here: https://files.me.com/stefanmelnychenko/453l4z

  • 写回答

1条回答 默认 最新

  • dqzd92796 2011-09-17 23:00
    关注

    This is where you copy from. You don't change variables or anything in order to copy any errors if any. sql echoed

    I checked your schemma and everything was fine, this is the php script:

    <?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);
    
    ?>
    

    and this is the result:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上