Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2010
    Posts
    45
    Plugin Contributions
    0

    help question Difficult sql - MAX(CASE)

    I'm trying to extract some order information from the database and display it on an EZ Page. I have done all the things to enable PHP for the EZ Page and it is working fine. I am "including" a php file that does the database access and displays the data I want.

    I have been plugging away at all this code and have pretty much got it all coming together. But I have reached an impasse with the sql. If someone can spot what I am doing wrong, it would be highly appreciated!

    I have tested the sql on phpAdmin and it works (well it has to be slightly different than as it would appear here) - I get the data I expect.

    I picked apart my included file to see where my problem lies. The looping etc is working just fine. The sql appears to execute and return the right number of rows. I tested this by simply outputting a string instead of any data, and the string was displayed the correct number of times.

    Whenever I attempt to manipulate or display data from the result set, the screen is blank from that point. I figure I must not be getting the Execute statement correct? I have tried adding in BindVars and that gives me an undefined variable type error. Therefore, I am pretty sure something is wrong in the sql or the functions executing the sql, producing an unusable result set (that has the right number of rows). Any ideas?

    Zen 1.3.8a
    Database Patch Level 1.3.8
    PHP 5.2.9
    Zend 2.2.0
    MySql 5.0.89

    Code:

    <?php

    $sql_list = "SELECT zo.customers_name, zop.products_id, MAX(CASE WHEN zopa.products_options_id = '11' THEN zopa.products_options_values END) fname, MAX(CASE WHEN zopa.products_options_id = '12' THEN zopa.products_options_values END) lname FROM " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . " zopa LEFT JOIN " . TABLE_ORDERS . " zo ON zo.orders_id = zopa.orders_id LEFT JOIN " . TABLE_ORDERS_PRODUCTS . " zop ON zop.orders_products_id = zopa.orders_products_id GROUP BY zopa.orders_id HAVING zop.products_id = '6'";

    // $sql_list = $db->bindVars($sql_list); ???????

    $sql_result = $db->Execute($sql_list);

    echo "<ul>";

    while (!$sql_result->EOF) {
    If $sql_result->fields['fname'] = '' {
    $sql_concat = $sql_result->fields['customers_name']);
    } else {
    $sql_concat = $sql_result->fields['fname'] . " " . $sql_result->fields['lname'];
    }
    echo '<li>' . $sql_concat . '</li>';

    $sql_result->MoveNext();
    }

    echo "</ul>";

    ?>

  2. #2
    Join Date
    Jun 2008
    Location
    Washington, DC
    Posts
    785
    Plugin Contributions
    7

    Default Re: Difficult sql - MAX(CASE)

    while (!$sql_result->EOF) {
    If $sql_result->fields['fname'] = '' {
    $sql_concat = $sql_result->fields['customers_name']);
    } else {
    $sql_concat = $sql_result->fields['fname'] . " " . $sql_result->fields['lname'];
    }
    while (!$sql_result->EOF) {
    If ($sql_result->fields['fname'] = ''){
    $sql_concat = $sql_result->fields['customers_name'];
    } else {
    $sql_concat = $sql_result->fields['fname'] . " " . $sql_result->fields['lname'];
    }

    You might want to load DrBytes debug tool that would help you.

    Skip
    • 446F63746F722057686F •

  3. #3
    Join Date
    Feb 2010
    Posts
    45
    Plugin Contributions
    0

    Default Re: Difficult sql - MAX(CASE)

    Thank you for spotting the missing brackets! That will help for sure. I will also check out this debug tool.

    I did want to note, that in my initial testing, I used a simple sql statement (just a couple of columns from the orders table) and those I was able to display with no problem.

  4. #4
    Join Date
    Feb 2010
    Posts
    45
    Plugin Contributions
    0

    Default Re: Difficult sql - MAX(CASE)

    Okay, I managed to figure things out. For those who are interested:
    • I had assigned token names to the evaluated columns retrieved in my sql, but not to the "ordinary" columns. When I added token names for the "ordinary" columns, all the columns were returned correctly. Before that, the "ordinary" columns returned NULL.

    • I changed the If clause to use the is_null() function and got the brackets done correctly, and then everything worked perfectly.

    It is definitely a nuisance when you have two un-related errors in a script!

  5. #5
    Join Date
    Jan 2008
    Posts
    1,700
    Plugin Contributions
    6

    Default Re: Difficult sql - MAX(CASE)

    If you want to compare something, shouldn't it be == instead of = ?

 

 

Similar Threads

  1. v151 Banner 7 being difficult
    By In2Deep in forum Templates, Stylesheets, Page Layout
    Replies: 4
    Last Post: 10 Jan 2015, 09:59 PM
  2. Case by case CSS button override. Possible?
    By runlikeagirl in forum Templates, Stylesheets, Page Layout
    Replies: 4
    Last Post: 3 Aug 2011, 02:34 AM
  3. Why is it so difficult?
    By igiveup in forum General Questions
    Replies: 3
    Last Post: 18 Mar 2011, 08:30 PM
  4. Add a quantity per case/master case
    By Bigpehr in forum Setting Up Categories, Products, Attributes
    Replies: 0
    Last Post: 15 Dec 2010, 09:40 PM
  5. 1064 SQL syntax Error - Max Value Changed ...HELP!!
    By Canvas101 in forum Setting Up Categories, Products, Attributes
    Replies: 3
    Last Post: 7 Oct 2007, 01:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR