webJestic.NET

Connecting to MySQL in Delphi for PHP (AppObsess)

preview image

In this video tutorial I begin building an ongoing project called AppObsess, which I will use as a generic and very simple application engine using the Smarty Template engine and the MySQL database. I begin this video series having applied the techniques from the other video’s:  Creating Editable Forms for Smarty Templates and Creating Dynamic Smarty Variables in Delphi for PHP.

Connecting to a MySQL database in Delphi for PHP is an extremely simple task.  In this video I create a data module and a config file containing the connection strings, include the files and execute the program.  That’s the entire video in a nutshell.  Most of the video is spent getting up to speed on the state of the application.

The core code is written in a file called maindb.php (the data module) and it looks like this:

  1. $MainData->MySQLDB->Host = HOSTNAME;
  2. $MainData->MySQLDB->DatabaseName = DATABASE;
  3. $MainData->MySQLDB->UserName = USERNAME;
  4. $MainData->MySQLDB->UserPassword = PASSWORD;
  5. $MainData->MySQLDB->Open();
  6. if (! $MainData->MySQLDB->Connected) {
  7.   echo ("Unable to connect to " . DATABASE . "\r\n");
  8.   exit ("maindb.php MainData->MySQLDB critical error");
  9. }

As always, you should have much better error control handling then I added for this simple tutorial. My connection testing was just to make sure something stupid didn’t happen while I was creating this video.

The config.inc file is very simple as well and only contains the connection strings for MySQL. It looks like this:

  1.    DEFINE(‘HOSTNAME’, ‘localhost’);
  2.     DEFINE(‘DATABASE’, ‘obsess’);
  3.     DEFINE(‘USERNAME’, ‘obsess’);
  4.     DEFINE(‘PASSWORD’, ‘demo’);

The starting template I am using for this AppObsess web framework in Delphi for PHP is the Blue Obsess template available from my XHTML/CSS Templates page.

The starting database SQL I have for this application is:

  1. DROP DATABASE IF EXISTS obsess;
  2. CREATE DATABASE IF NOT EXISTS obsess;
  3. USE obsess;
  4.  
  5. CREATE TABLE page (
  6.         id bigint(20) NOT NULL AUTO_INCREMENT,
  7.   page_name varchar(200) NOT NULL,
  8.   page_body text NOT NULL,
  9.  
  10.         UNIQUE KEY page_name (page_name),
  11.   PRIMARY KEY (id, page_name)
  12. ) TYPE=InnoDB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1000;

If you would like to download this application in the state it is for this video, you can download it using the link below. The .SQL file, the template, and the Delphi for PHP project code is all included.

Free Membership Required: Please Login or Register to read the rest of this content.

Comments

2 Responses to “Connecting to MySQL in Delphi for PHP (AppObsess)”
  1. alex zan says:

    Hi, I’m coming from many years of Delphi Win32 experience and I was looking for a starter in Delphi 4 php.
    Your videos provided what I was looking for – please keep ‘em coming and thanks a lot,
    Alex

  2. Michael says:

    I plan to create more. I’m currently in China and working on a few things, and the quality of my sound might not be that great but I am putting more video together for Delphi for PHP development, as well as other things.

    Thanks!

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

webJestic.NET