Zen Cart Logo
Forums / Bug Reports / Strange behavior with print_r() with query_factory results

Strange behavior with print_r() with query_factory results

Views: 814

Results 1 to 6 of 6
17 Mar 2020, 21:49
#1
davewest avatar

davewest

Totally Zenned

Join Date:
Dec 2007
Location:
Payson, AZ
Posts:
1,075
Plugin Contributions:
7

Strange behavior with print_r() with query_factory results

clean install zc1.5.6c
XAMPP for Linux 7.3.7-1
This version of XAMPP contains the following software releases:
- Apache 2.4.39
- MariaDB 10.3.16
- OpenSSL 1.0.2s
- PEAR 1.7.1
- PHP 7.3.7

  • phpMyAdmin 4.9.0.1

Also a heavy modified site in zc1.5.6c
on a older xampp with php7.3.1

Working on modifying logic of return manager. I was dumping database querries to see results with

      $opd_query  = "SELECT opd.orders_id, opd.orders_products_id, opd.orders_products_filename, opd.download_maxdays, opd.download_count, opd.products_attributes_id,
                                 pa.orders_id, pa.orders_products_attributes_id, pa.orders_products_id, pa.attributes_images
                         FROM   " . TABLE_ORDERS_PRODUCTS_DOWNLOAD . " opd,  
                                " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . " pa
                         WHERE  opd.orders_id = :ordersID  
                         and    opd.orders_id = pa.orders_id  
                         and    opd.products_attributes_id  =  pa.orders_products_attributes_id";


                             
      $opd_query = $db->bindVars($opd_query, ':ordersID', $orderID, 'integer');
      $download_orders = $db->Execute($opd_query);   


die(‘<PRE>Downloads ‘ . print_r($download_orders, TRUE) . ‘</PRE>’); 

The query expected to list two rows and end.. Instead, I got one row and a mysqli dump.

Downloads queryFactoryResult Object
(
    [EOF] =>  
    [cursor] => 0
    [fields] => Array
        (
            [orders_id] => 1044
            [orders_products_id] => 62
            [orders_products_filename] => Quad_ADAMS MESA.pdf
            [download_maxdays] => 7
            [download_count] => 1
            [products_attributes_id] => 49
            [orders_products_attributes_id] => 49
            [attributes_images] => maps/Quad_ADAMS_MESA.jpg
        )


    [is_cached] => 
    [result] => Array
        (
        )


    [result_random] =>  
    [limit] =>  
    [resource] => mysqli_result Object
        (
            [current_field] => 0
            [field_count] => 10
            [lengths] => Array
                (
                    [0] => 4
                    [1] => 2
                    [2] => 19
                    [3] => 1
                    [4] => 1
                    [5] => 2
                    [6] => 4
                    [7] => 2
                    [8] => 2
                    [9] => 24
                )


            [num_rows] => 2
            [type] => 0
        )


    [link] => mysqli Object
        (
            [affected_rows] => 2
            [client_info] => mysqlnd 5.0.12-dev - 20150407 - $Id: 401a40ebd5e281cf22215acdc170723a1519aaa9 $
            [client_version] => 50012
            [connect_errno] => 0
            [connect_error] =>  
            [errno] => 0
            [error] =>  
            [error_list] => Array
                (
                )


            [field_count] => 10
            [host_info] => Localhost via UNIX socket
            [info] =>  
            [insert_id] => 0
            [server_info] => 5.5.5-10.1.37-MariaDB
            [server_version] => 50505
            [stat] => Uptime: 3186  Threads: 1  Questions: 892  Slow queries: 0  Opens: 64  Flush tables: 1  Open tables: 58  Queries per second avg: 0.279
            [sqlstate] => 00000
            [protocol_version] => 10
            [thread_id] => 13
            [warning_count] => 0
        )


    [sql_query] => SELECT opd.orders_id, opd.orders_products_id, opd.orders_products_filename, opd.download_maxdays, opd.download_count, opd.products_attributes_id,
                                 pa.orders_id, pa.orders_products_attributes_id, pa.orders_products_id, pa.attributes_images
                         FROM   zen_orders_products_download opd,  
                                zen_orders_products_attributes pa
                         WHERE  opd.orders_id = 1044  
                         and    opd.orders_id = pa.orders_id  
                         and    opd.products_attributes_id  =  pa.orders_products_attributes_id

However, if I do a foreach as bellow, the results are as expected two rows of data..

echo '<pre>downloads ';
foreach ($download_orders as $row) {
   print_r($row);
}
echo '</pre>';
die('done');  


downloas Array
(
    [orders_id] => 1044
    [orders_products_id] => 62
    [orders_products_filename] => Quad_ADAMS MESA.pdf
    [download_maxdays] => 7
    [download_count] => 1
    [products_attributes_id] => 49
    [orders_products_attributes_id] => 49
    [attributes_images] => maps/Quad_ADAMS_MESA.jpg
)
Array
(
    [orders_id] => 1044
    [orders_products_id] => 63
    [orders_products_filename] => NAIP_ADAMS MESA.pdf
    [download_maxdays] => 7
    [download_count] => 1
    [products_attributes_id] => 50
    [orders_products_attributes_id] => 50
    [attributes_images] => maps/NAIP_ADAMS_MESA.jpg
)


done

With a zc1.5.5f I had no problems getting a simple query to print out..

Wondering if it’s me or something changed in 1.5.6c

17 Mar 2020, 23:20
#2
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,953
Plugin Contributions:
8

Re: Strange behavior with print_r() with query_factory results

hey dave,
i can't tell you about the letter designations, but it does look like the execute function has changed between v155 and v156. you can see the execute function here for v156:

https://github.com/zencart/zencart/blob/22424cedd39d8b4b8496438507f8a897881e0644/includes/classes/db/mysql/query_factory.php#L176

you can change the version to v155 to see the differences.

the execute function returns an object as opposed to an array. but the return of an object is consistent between versions (the object itself is probably different, although i did not dig).

if you are using the pre html tags and print_r php calls, may i suggest dBug:

https://dbug.ospinto.com/

a great little tool for debugging php code and doing the sort of thing you are doing. it has not been updated in a bit, and you need to rename the dBug function to _construct for use with php7.2 and greater (or whatever) but i love it as a php tool.

i just add the file in includes/extra_configures and then put in:

new dBug($download_orders);
die(__FILE__ . ':' . __LINE__);

and i am off to the races. i like the die statement showing which script and lineup i put it on. makes it easy to take it out!

debugging the db class of ZC, while educational, might lead to nothing of interest. it is such a CORE part of ZC, that if there were problems with it, i am sure we would hear about it further.

oh, and one other thing, print_r "may" have changed between versions of php as well.... you would need to look into that if truly curious.

hope that helps!

best.

17 Mar 2020, 23:34
#3
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Strange behavior with print_r() with query_factory results

davewest, what you're seeing is normal.

I'm surprised that you're claiming that 1.5.5 gave different output.

18 Mar 2020, 02:24
#4
davewest avatar

davewest

Totally Zenned

Join Date:
Dec 2007
Location:
Payson, AZ
Posts:
1,075
Plugin Contributions:
7

Re: Strange behavior with print_r() with query_factory results

DrByte:

davewest, what you're seeing is normal.

I'm surprised that you're claiming that 1.5.5 gave different output.
First time I had this type of a dump with print_r..

Is this printing an object and not what I was expecting, string or array? Which would explain why foreach ($return_orders as $row) worked..

@carlwhat
checking versions was my first thought before posting... also tried different versions of php and the history of print_r,,

18 Mar 2020, 06:45
#5
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Strange behavior with print_r() with query_factory results

Several major versions ago the ability to foreach() through query results was added. But calling print_r() on a query result has always displayed an object, not merely the individual records.

Perhaps you're confusing it with a direct mysql_query result, which Zen Cart insulates you from (always has)?

18 Mar 2020, 14:57
#6
davewest avatar

davewest

Totally Zenned

Join Date:
Dec 2007
Location:
Payson, AZ
Posts:
1,075
Plugin Contributions:
7

Re: Strange behavior with print_r() with query_factory results

DrByte:

Several major versions ago the ability to foreach() through query results was added. But calling print_r() on a query result has always displayed an object, not merely the individual records.

Perhaps you're confusing it with a direct mysql_query result, which Zen Cart insulates you from (always has)?

Figured it was something with me... Thanks..