2 Caveats To Be Aware of When Creating Your Own osCommerce Package


Whilst working on my Power Pack, I discovered two limitations of the osCommerce install process.

The default osCommerce package comes complete with a SQL file containing all the tables and sample data needed to populate a database. This file can be found at catalog/includes/install/oscommerce.sql.

I wanted to add extra SQL commands that were needed by my contributions into this file, so the installation process would do everthing.

This way anyone installing my osCommerce Power Pack would not need to perform additional tasks having been through the default install process (great for new users who aren’t all that technically savvy).

Having added my commands to the end of oscommerce.sql, I proceeded through the default install process only to find some of my commands were not being executed.

Hmm…

I wrestled for several hours looking for errors, before realising that it was not my changes causing problems. It was the parsing algorithm in the install script.

In case you ever intend to customise your installation files, you should be aware of these two caveats. Knowing them might save you a few headaches.

Caveat 1. Only CREATE, INSERT and DROP commands get executed

If you want to run other commands, you need to make a change to the osc_db_install function in catalog/includes/install/includes/functions/database.php.

Open this file and find the following code around line 191:

  1. if ( (eregi(‘create’, $next)) || (eregi(‘insert’, $next)) || (eregi(‘drop t’, $next)) ) {
  2.     $next = ;
  3.     $sql_array[] = substr($restore_query, 0, $i);
  4.     $restore_query = ltrim(substr($restore_query, $i 1));
  5.     $sql_length = strlen($restore_query);
  6.     $i = strpos($restore_query, ‘;’)-1;
  7. }

The if statement searches each line in the oscommerce.sql file for the string create, insert or drop. If the line does not contain any of these strings it is ignored.

One of my commands was an ALTER statement and this code was causing it to be discarded and never executed.

The Solution - Add to the IF statement expression

You need to add additional OR expressions to the end of the if statement for each of the commands you want to run.

  1. if ( (eregi(‘create’, $next)) || (eregi(‘insert’, $next)) || (eregi(‘drop t’, $next)) || (eregi(‘alter’, $next)) ) {

By adding eregi(’alter’, $next) to the if statement, all ALTER statements will now be parsed correctly and executed.

Caveat 2. If the last line of oscommerce.sql is a comment, bad things happen…

Make sure you don’t put any comments after the final SQL statement. If you do, that last SQL statement will not execute.

Consider the example below.

  1. INSERT INTO zones VALUES (18,223,‘FL’,‘Florida’);
  2. # This comment is below the final SQL statement. As a result, poor old Florida will not be inserted into the zones table.

Because of the comment below the final SQL statement, the INSERT command will not be executed.

Weird, I know.

This is caused by a bug in the parsing algorithm. It can be fixed but it’s far simpler and more time efficient to just not put comments there in the first place.

Conclusion

So there you have it - two things to watch out for if you plan to create your own osCommerce package. Both are easy to work around provided you understand what is causing the issues.

Hopefully reading this article will save you a few keyboard-bashing, mouse-throwing hours in the future.

Enjoyed reading this post? Get more delivered directly to you.

Related Posts


Responses

  1. There are currently no responses.

Reply

(required)

(will not be published) (required)