Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6605767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:19:24+00:00 2026-05-25T19:19:24+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:19:24+00:00Added an answer on May 25, 2026 at 7:19 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following simple MySQL query, called from PHP: SELECT foo_id, SUM(number_of_guests) FROM
I have the following MySQL query that I execute from a .php page SELECT
.i have the following code: $task_id = mysql_query(SELECT MAX(task_id) from it_task); echo $task_id; .what
I have the following which returns soccertennisfootball $interestsquery = SELECT * FROM user_interests WHERE
I have the following MySQL query: SELECT (COUNT(id) * 5) AS total FROM content
I have the following MySQL query and tables from which I am querying: SELECT
I have the following mysql query which I am running with php like so.
I have the following table: mysql> SELECT * FROM `bright_promotion_earnings`; +----+----------+------------+----------+-------+ | id |
I have the following MySQL query: SELECT value_1, ( SELECT value_4 FROM table_1 WHERE
I have a php page which displays class schedule data for each user from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.