Zen Cart Logo
Forums / General Questions / Need help for fix MySQL to MySQLi Error on old moudles

Need help for fix MySQL to MySQLi Error on old moudles

Views: 2,115

Results 1 to 11 of 11
20 Apr 2016, 10:12 AM
#1
explorer1979 avatar

explorer1979

Inactive

Join Date:
Jan 2007
Posts:
377
Plugin Contributions:
0

Need help for fix MySQL to MySQLi Error on old moudles

Hi,

I am upgrading a ZC 1.5.1 to ZC 1.5.4

The ZC 1.5.4 logs see some log like this
PHP Warning: mysql_query(): Access denied for user 'user'@'localhost' (using password: NO) in /home/user/public_html/zc/admin/shipping_export1.php on line 1194

PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/user/public_html/zc/admin_xx/shipping_export1.php on line 1195

PHP Warning: mysql_query(): Access denied for user 'user'@'localhost' (using password: NO) in /home/user/public_html/zc/admin/shipping_export1.php on line 1198

PHP Warning: mysql_query(): A link to the server could not be established in /home/user/public_html/zc/admin/shipping_export1.php on line 1198

Yes, yes it is a Export Shipping+Order Information
but .... since some reason, I need change it to match my need, so above file named shipping_export1.php

While I google some inform know that from ZC 1.5.3 starting move from MySQL to MySQLi

And I also see the
Zen Cart’s database abstraction layer
https://www.zen-cart.com/wiki/index.php/Developers_-_Database

But ... not too understand how to fix my self coding

Here are my coding that can work on ZC 1.5.1, but not the ZC 1.5.4
where the shipping_export1.php on line 1194 is below "$yu"

<?php
                $payment_term=$module->title;
                $payment_method=(strstr($module->code, 'paypal') ? 'PayPal' : $module->code);
                $yu=mysql_query("SELECT * from required_definitions where payment_method = '".$payment_method."'  ");
                $req=mysql_fetch_array($yu);
                if($req['payment_method'] != $payment_method)
                {
                mysql_query("INSERT into required_definitions (payment_method,class,class_name,definition) VALUES ('".$payment_method."','','','')");    
                }
                
              ?>

and after I am read the
Zen Cart’s database abstraction layer
https://www.zen-cart.com/wiki/index.php/Developers_-_Database

I had try change the line 1194 to

$yu=$db->Execute("SELECT * from required_definitions where payment_method = '".$payment_method."'  ");

then the logs not show error on line 1194 anymore, but ... I still see error on line 1195, 1198, no example for me as reference, I am lost of idea how to change it using $db->Execute way .. :frusty:

Any one can help me fix it? Thank you.

20 Apr 2016, 10:21 AM
#2
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,689
Plugin Contributions:
56

Re: Need help for fix MySQL to MySQLi Error on old moudles

<?php
                $payment_term=$module->title;
                $payment_method=(strstr($module->code, 'paypal') ? 'PayPal' : $module->code);
                $yu=$db->Execute("SELECT * from required_definitions where payment_method = '".$payment_method."'  ");
                $req=$yu->fields['payment_method'];
                if($req != $payment_method)
                {
                $db->Execute("INSERT into required_definitions (payment_method,class,class_name,definition) VALUES ('".$payment_method."','','','')");    
                }
                
              ?>
20 Apr 2016, 10:28 AM
#3
design75 avatar

design75

Totally Zenned

Join Date:
Dec 2009
Location:
Amersfoort, The Netherlands
Posts:
2,862
Plugin Contributions:
5

Re: Need help for fix MySQL to MySQLi Error on old moudles

Looks like the rest of the code may also need an overhaul, because the table names are also hard coded.

20 Apr 2016, 10:30 AM
#4
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,689
Plugin Contributions:
56

Re: Need help for fix MySQL to MySQLi Error on old moudles

If it worked before, it means he's not using a prefix. I agree this is not best practice, but there you are.

One thing @explorer1979 should be on the lookout for is missing sanitization of input values, which was common in older modules.

21 Apr 2016, 2:12 AM
#5
explorer1979 avatar

explorer1979

Inactive

Join Date:
Jan 2007
Posts:
377
Plugin Contributions:
0

Re: Need help for fix MySQL to MySQLi Error on old moudles

swguy,

Thank you, your coding work for me.
But one more question more ....

I using ZC Build-in Developer Tools try search "->fields"

See some example using this
$somereq=$something->fields;

Do
```php
$req=$yu->fields['payment_method'];


can be
```php
$req=$yu->fields;
if($req['payment_method'] != $payment_method)
```

this way? if yes, why? or where can find the function define on ZC of this ->filed object?

Sorry look like a bit more questions, I am still learning PHP, now working on PHP function this learning, for PHP OOP and object I am still a new comer, want to know more ... since I know ZC 1.6 or up will be more OOP way ..

So, it is time to learning more from this ..

Thank you of your value time and help.
21 Apr 2016, 2:14 AM
#6
explorer1979 avatar

explorer1979

Inactive

Join Date:
Jan 2007
Posts:
377
Plugin Contributions:
0

Re: Need help for fix MySQL to MySQLi Error on old moudles

design75,

Hi, thank you of your suggestion, I write this since I using phpmyadmin most time..
Directly using it SQL tab to write SQL, so if can work I will copy and paste on it.
And like what I said before, this is my self need custom coding for my shop output some special CSV, not include on original "Export Shipping+Order Information" official ZC module.

21 Apr 2016, 9:32 AM
#7
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,689
Plugin Contributions:
56

Re: Need help for fix MySQL to MySQLi Error on old moudles

explorer1979:

I using ZC Build-in Developer Tools try search "->fields"

See some example using this
$somereq=$something->fields;

Do
```php
$req=$yu->fields['payment_method'];

> 
> can be
> ```php
$req=$yu->fields;
if($req['payment_method'] != $payment_method)
```

This would be different than all the other code you see in Zen Cart.  It's really best to keep with the coding standard you see in all the other files.
21 Apr 2016, 12:15 PM
#8
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,069
Plugin Contributions:
56

Re: Need help for fix MySQL to MySQLi Error on old moudles

explorer1979:

swguy,

Thank you, your coding work for me.
But one more question more ....

I using ZC Build-in Developer Tools try search "->fields"

See some example using this
$somereq=$something->fields;

Do
```php
$req=$yu->fields['payment_method'];

> 
> can be
> ```php
$req=$yu->fields;
if($req['payment_method'] != $payment_method)
```
> 
> this way? if yes, why? or where can find the function define on ZC of this ->filed object?
> 
> Sorry look like a bit more questions, I am still learning PHP, now working on PHP function this learning, for PHP OOP and object I am still a new comer, want to know more ... since I know ZC 1.6 or up will be more OOP way ..
> 
> So, it is time to learning more from this ..
> 
> Thank you of your value time and help.
When you see the PHP construct ***$yu->fields*** within Zen Cart code, there's usually a preceeding call to a ***$db*** (database object) function that requests information from the database similar to ***$yu = $db->Execute ("SELECT <field list> FROM " . TABLE_SOMETHING);***

Each associative array-element of the fields value returned "maps" to an entry in the ***<field list>*** of that SQL query.
21 Apr 2016, 3:41 PM
#9
mc12345678 avatar

mc12345678

Totally Zenned

Join Date:
Jul 2012
Posts:
16,908
Plugin Contributions:
2

Re: Need help for fix MySQL to MySQLi Error on old moudles

Using the above:

$req=$yu->fields['payment_method'];

And

$req2=$yu->fields;

Then:

$req===$req2['payment_method']

The difference is that $req is a single value, where $req2 is an array of values. So there is a bit of a trade-off and potential need for either of these. It would be a waste of memory and related processing time to store the array if the query returns many values but only one is ever used.

Also, problems can arise in the code if the array is maintained through a long series of code that could potentially modify one or more keys of the variable. By working with one portion of the total the code can typically be easier to follow and modify without impacting the entire program.

By using the first method as suggested by swguy, the code tends to be easier to read, to understand, and more importantly to debug.

21 Apr 2016, 4:06 PM
#10
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,689
Plugin Contributions:
56

Re: Need help for fix MySQL to MySQLi Error on old moudles

mc12345678:

...
The difference is that $req is a single value, where $req2 is an array of values. So there is a bit of a trade-off and potential need for either of these. It would be a waste of memory and related processing time to store the array if the query returns many values but only one is ever used.

This is almost certainly more than the OP wanted to know, but:

There is no additional cost in memory or processing time for method 2. You have already paid the price for retrieving and storing these variables. To reduce memory and processing time, you could change

                $yu=$db->Execute("SELECT * from required_definitions where payment_method = '".$payment_method."'  ");

to

                $yu=$db->Execute("SELECT payment_method from required_definitions where payment_method = '".$payment_method."'  ");

since the field "payment_method" is the only one used.

That would save the additional overhead of retrieving the additional fields in the required_definitions table.

22 Apr 2016, 1:57 AM
#11
explorer1979 avatar

explorer1979

Inactive

Join Date:
Jan 2007
Posts:
377
Plugin Contributions:
0

Re: Need help for fix MySQL to MySQLi Error on old moudles

Hi all,

Thank you, I learning more from all of your kindly reply.
And know how the coding also effect memory/resource using right now that I also thinking of while writing them. (thx mc12345678 and swguy)

Also Thank you of lat9 that answer my question more detail.