Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
This month's Developer Shed Writing Contest Winner, Nicholas Clayton, explains how he was
able to use VB.NET, PHP, and XML to talk to a troublesome MySQL database. "There may be
times when what your client needs and what their web host will give you brings your project to a
grinding halt. I have encountered such a problem recently when trying to access a client’s
hosted MySQL Database from an app I have written in VB.NET."
No matter what I did, the server would not let me in. I had tried connecting from my web site
first, but that was a no go. So I decided to break out a VB.NET app to see if that would do the
trick. Well, it does, but not the way I was hoping for. I have finally finished a workaround for
this security nightmare that I have had for the past couple of weeks.
I had tried talking to the hosting company but kept getting very general, very basic answers that
really did not have anything to do with what I was trying to accomplish. So after banging my
head on the desk and scouring Google like a madman, I decided to take matters into my own
hands and learn a few new tricks while I was at it.
First, I will explain the big picture and then break it down into the individual pieces.
The Big Picture
I’ve created a small front-end in VB.NET to display a DataGrid showing messages saved in the
client’s MySQL database. I cannot connect through MyODBC thanks to the wonderfully secure
hosting environment, so I came up with another way…XML generated by PHP. Don’t ask me
how I came up with this, it was a late night enlightenment.
In order to limit who or what could access this information, I created a key value to pass along
with the rest of my post data. Once this data has been posted, I get back a string of XML with the
data I need for the DataGrid. This is similar to the actions that occur when communicating with a
Web Service.
Simple enough, right?
First, I’ll start with the front-end.
You will also need to create a new class named WebBridge.vb. Since it is the most important
piece of the front-end, we will start here. Refer to the comments within the code for
explanations.
This will handle all of the communication with the target php page as you will see shortly. There
are only a few procedures needed for frmMsg. These will establish a connection to a target php
page through a WebBridge object and will then display the appropriate information in our
DataGrid. Our DataGrid’s table will be structured like our XML output from the target php page.
You will see the connection later in the PHP section of the tutorial.
Now that this is out of the way, we can create our php page to generate the XML. We will
assume that data has been entered into the database through a different php page, so all we are
concerned about is pulling the data that already exists. The steps we need to follow to generate
the XML file are:
1) Check for an Access Key.
2) Validate the Access Key. In our case, we are checking for a value of "12345".
3) Start the XML document.
4) Connect to MySQL and select the proper database.
5) Query the table for the desired information.
6) Loop through the recordset and create XML containing the retrieved data.
7) Close the database connection
8) Close the XML document.
<?
//getmessagelist.php
Echo "</Messages>";
}
?>
<Messages>
<Message>
<MsgID>1</MsgID>
<From>Test Dummy</From>
<Email>testdummy@td.com</Email>
<MsgDate>1/14/2004</MsgDate>
<MsgTime>1073437277</MsgTime>
</Message>
</Messages>
A clean and valid xml string containing all of your requested data that can be used to easily fill a
DataSet within VB.NET. Sweet and (kind of) simple. Now run your project, click on either the
“Read New” button or “Read Old” button to view the data from the database.
Your datagrid will show you something like this:
Now you can pat yourself on the back for peacefully bringing together 3 distinct technologies
into one harmonic package.
Conclusion
If you need more information about any of the discussed technologies (VB.NET, PHP, MySQL,
XML) be sure to visit these resources out on the web:
VB.NET - http://msdn.microsoft.com/vbasic
PHP - http://www.php.net
MySQL - http://www.mysql.com
XML - http://www.xml.com
So, if you are ever caught with an overly secure MySQL server environment and need to pull
some data remotely, you can do it will a little bit of .NET magic.
This is meant as an example, and not as a full blown, secure method for database
communications. This is also a good way to become familiar with some VB.NET techniques and
to play around with various other technologies in a single project. I know I have learned quite a
bit from trudging through this exercise myself and hopefully this will help others out there
looking to do the same, while also catching the attention of Developer Shed, who will may
just deem this worthy of display. [And in fact, we have! -Editor]