Director – MySQL communication

As a person whose experience in programming is connected mainly to Director and Lingo, I often searched but never found a comparatively simple and clear tutorial that describes step by step what is necessary to do in order to get an application, which communicates in a duplex way to a MySQL database. Here I will try to explain how to achieve this on your local machine, under Windows – if the reader has already read such tutorials he or she may skip the following lines.
Generally, to get this you need: a web server, a MySQL server, a database, a Director application, as well as something for the communication between the application and the MySQL server. In our case this will be PHP.
For our convenience and quickness, in this tutorial we will use XAMPP and thus we will get in one fling a web server ( working with PHP4 and PHP5 ) and MySQL server.

PART ONE

1. Download and install XAMPP. Leave all the settings by default. After the installation process is finished a directory “c:\xampp\” must have been created.

2. In the directory “C:\xampp\htdocs\” create another directory called “DirectorTest\”. After that download this file

Director Test Database (9929 downloads)

and save it in the directory.

3. Choose Start > Programs > XAMPP For Windows > XAMPP Control Panel and start Apache and MySQL. Now you have a working web server, PHP and a MySQL server on your local machine. Test them by opening the browser and try on this address: “http://localhost”. You should see the initial screen of “XAMPP for Windows” – choose a language.

4. Choose PHPMyAdmin, write “directortest” in Create new database text field and click Create.

5. Click Import tab, after that click Choose File button and select “DirectorTestDatabase.sql” file, downloaded and saved in step 2
( The full path to it should be “c:\xampp\htdocs\DirectorTest\DirectorTestDatabase.sql” ) Click GO button. You should see a notice that says “Import has been successfully finished, 16 queries executed.” Now you have a database. There is a table in this database, called “addressbook”, which contains the following columns: id, first_name, last_name, e_mail, phone_number

6. Now here comes the Director application. To spare time, there is an already finished one – download it and save it in “c:\xampp\htdocs\DirectorTest\”

Address Book (2716 downloads)

7. Start AddressBook.dir in Director. You will probably see an alert:

"Warning:  require_once(DiDal/DiDalHttpHandler.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in C:\xampp\htdocs\DirectorTest\didal.php on line 2 Fatal error:"

8. Now we will introduce DIDAL – Director Data Access Layer – to the whole picture. With Director still opened, download and unzip it in the already familiar directory “c:\xampp\htdocs\DirectorTest\”.

DiDAL (7749 downloads)

After unzipping, the directory should look as follows:

DiDal\
AddressBook.dir
didal.php
didaltest.php
DirectorTestDatabase.sql

Start the “AddressBook” application again – now it should look like this:

PART TWO

The following is the recommended way to work with DIDAL:
1. We carefully consider the interactions between the application and the MySQL server and on this basis we define “commands” with parameters. In this particular case the interactions of the application with the server can be reduced to: extracting all the records from the table, called “addressbook”, adding new records to “addressbook” table and deleting records from “addressbook” table. In this case, our command names are sufficiently descriptive – “getAllContacts”, “createNewContact” and “deleteContactById”.
2. We specify the data (parameters) needed for a command to be executed. For example, the execution of “getAllContacts” command does not need any additional data supplied by the application. Obviously, “createNewContact” needs data to be recorded in “addressbook” table. The execution of “deleteContactById” is impossible without supplying “id” of the record which must be deleted.
3. After specifying all this, we open “didal-config.xml” file, which should be in “c:\xampp\htdocs\DirectorTest\DiDal\didal-config.xml”. Pay attention to this line:

<connection host="localhost" username="root" passwd="" dbname="directortest" encoding="cp1251" />

This assigns parameters to the connection with the database.
In section, we define our commands as follows:

<command name="yourCommandName" table="databaseTableName">
			<!&#91;CDATA&#91;
				YOUR SQL QUERY;
			&#93;&#93;>
</command>

So, we see that in this file the two commands – getAllContacts and createNewContact are defined. deleteContactById is not defined. That is why when the execution is started the following alert is generated:

"Fatal error:  Call to a member function getTable() on a non-object in C:\xampp\htdocs\DirectorTest\DiDal\DiDalRequest.php on line 55"

Here we define deleteContactById command:

		<command name="deleteContactById" table="addressbook">
			<!&#91;CDATA&#91;
				DELETE FROM addressbook WHERE id = :rowid
			&#93;&#93;>
		</command>

Notice that parameters are written with “:” at the begining. We define the command as

IO.registerCommand( "deleteContactById", [ #rowid ] )
in the application. But in the configuration file #rowid parameter is written as :rowid
Have in mind that “table” attribute in

<command name="deleteContactById" table="addressbook">

line will be converted to a symbol when the result of the command is returned. It is necessary this attribute to be present, but it is not necessary for it to correspond to the name of the table, which we reffer in the SQL request.

PART THREE

At the end we must create a projector in order to have a working desktop application. For this purpose you have to create directory c:\xampp\htdocs\DirectorTest\Xtras\
Copy the following files in it:

Proj.dll
msvcrt.dll
Iml32.dll
Dirapi.dll

which can be found in the installation directory of Director ( for example “C:\Program Files\Macromedia\Director MX 2004\” )
Copy “NetLingo.x32”, “INetURL.x32”, “NetFile.x32” files in Xtras directory.
“INetURL.x32” and “NetFile.x32” are in “Your root Director Folder\Configuration\Xtras\Core\Net Support\”
“NetLingo.x32” can be found in “Your root Director Folder\Configuration\Xtras\Scripting\”
In Director, choose from the main menu File > Publish…
In “Select Output directory for AddressBook.exe” dialogue box select “C:\xampp\htdocs\DirectorTest\” directory and click Select Folder.

Here is a working demo:

Director-DiDal-MySQL demo
 
Comments

Great stuff dude. Sounds really amazing to me…

Thanks, useful/clear explanation

Scott Whitney

I use Director MX 2004 – never had a real reason to upgrade.
I found one CRITICAL bug that was driving me bonkers.
In the PHP code to convert the mySQL query results to a list, I found that I MUST use single quotes around strings. Double quotes work fine from the message window, but in runtime Director gets confused and breaks down unless I use single quotes.
Thanks TONS for your outstanding tutorial. It really helped me switch from using the Rosetta XTRA with MSAccess to mySQL both online and on a local flash drive.

Scott Whitney

Just to clarify what I mean by using single quotes:
echo “[“; // this works fine in the message window, but not in runtime
echo ‘[‘ // this works in runtime

Leave a Reply