Services: 1.877.633.9536 ext. 4
Sales: 1.877.633.9536 ext. 2

Site Map | Contact Us | Our Portfolio| Home
Programming Languages
  :: Computer Programming

:: HTML Programming

:: Web Programming

:: PHP Programming
  PHP / MySQL Programming
MySQL / Access Conversion

:: Flash Programming

:: Linux Programming



<-- Back to Programming





 





PHP / MySQL Programming

PHP is one of the hottest scripting languages to be found on the Internet today. The fact that PHP code can be inserted directly alongside HTML makes the language all the more convenient.

Much of PHP is a combination of Perl, Java and C concepts. Some features of using PHP include performing sophisticated mathematical calculations, providing network information, offering mail and regular expression capabilities, and much more.

PHP's strongest feature is its database interfacing capability. Connecting a database to the Internet has never been so easy. What's more, it supports many of the most popular database servers on the market, including MySQL (perhaps the most powerful database server found on the market today).

With MVI Solutions's advanced PHP and MySQL programming you will get a top-notch dynamic, database-driven Web site.

Learn More About PHP / MySQL Programming

PHP and MySQL it's common to just jump right in and start building your MySQL tables with PHP My Admin and not make any considerations for future database growth or needs. MVI Solutions recommends that you follow so prudent ways to build these tables and relationships.

The following example will extend a MySQL database in the form of a PHP class. Basically, if you want to add a field to your MySQL database, the PHP class acts as a wrapper to your database and adds the field for you. Sound complex? Well, this must be done properly but it will add efficiencies to your programs.

MVI Solutions uses PHP 4.1 and MySQL 3.23 or above. We are running a Linux Web server, but everything will also work on a Windows 2000 server with Apache/IIS.

When developing Web applications, it is tedious to add fields to my a MySQL table as a new ideas appear.

The general idea is just to define a PHP class that contains the SQL-structure of your MySQL database, and have another object update the tables in your database automatically.

This is the WS Document and is a class to implement a document for my publishing engine.

This extends WS Dynamic Object line, which is the object that handles mySQL communication. Now, whenever we want to add a new field to my table for this class, It is simply.

What's the Magic?

Because we have added all the table fields of my class into my object WS Dynamic Object, we can use that list of fields to generate an automatic load and save function. Imagine the tedious task of handling a submit form and saving the result into your MySQL database. The problem is easily solved with these dynamic classes. The object WS Dynamic Object runs through the list of fields you have defined in your class, and checks if this field is in the HTTP_GET_VARS or HTTP_POST_VARS array, if it is, then it loads the values to an instance of your class and runs the save() function.

How about updating records? Well, if you add a hidden field with the name of your unique field and the unique field value as its value, then the instance of your class will be given the unique value and the correct record will be updated. If this value is -1, then a new record is inserted into the database.

We've already seen how we add fields to the WS Dynamic Object class, but how does it work? This shouldn't be too hard to understand, simply showing the variables of the class and the add Field function. The save() algorithm that is used in form submission handling. The load() algorithm that is called in the constructor of my WS Document class. This simply runs through the record set that it fetched from the MySQL table and inputs the values into the object.

It says that the object is no longer new, and that it has loaded values - this makes sure that when the object is saved, the record set is updated and not inserted.

Here comes the tricky part -- keeping the structure defined in the object and the structure of the database and its tables in sync. The entire function, called syncTable works... believe us!

Here's a basic overview of what the code does:
  • Create the table if it doesn't exist.

  • Get the list of fields from the table and store them in an array.

  • Run through the list of fields and match the names.

  • If the names match, see if the field types match (ex: int(11) == int(11) ).

  • If the field types match, nothing needs to be done.

  • If they don't match, update the field so they match.

  • If the field name is not found, then add the field to the table.

  • That's it! But there are some details I left out when showing you the class WS Dynamic Object.
Here is the complete variable list of the class:
  • class WS Dynamic Object {
  • var $fields; // an array of the fields in this structure
  • var $newobject; // is this a new object or loaded values from db?
  • var $table_fields; // the database equivalent of $this->fields
  • var $fields_ok; // control-list during update
  • var $missing_fields;// a list over the fields not added to the table
  • var $formmode;
The class variable called $formmode. The tricky part is to check if the field is check or not checked, especially with the save() function we use in this object. To solve this, I added another method to WS Dynamic Object called add Form Mode Field.

Although the naming of the function could have been better, the functionality works, and the method used is from the WS Document class.

This simply says that the field published should be treated as a checkbox, so when we add a form to create or update an instance of WS Document.

The function formMode() runs through the array formmode, and set the value to checked if the value is 1 and sets it blank if the value is 0. When the object is saved, the reversed action makes sure that checked = 1 in your MySQL table.





MVI Solutions © 2006 All Rights Reserved
6301 NW 5th Way, Suite 450 | Fort Lauderdale, FL 33309
For information call 1.877.633.9536 or email: info@mvisolutions.com