Bridging the Gap: Talking to MySQL From VB.NET Through PHP and XML - PHP: Talking the Universal Language
(Page 3 of 4 )
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
// Make sure that there is an
//Access Key in the post data
If($_POST['acc'] != '')
{
If($_POST['acc'] == "12345")
// Check for the correct Key
$auth = 1;
// Key is correct, this is a
//valid request
Else
$auth = 0;
//Invalid request
}
Else
$auth = 0;
//Invalid request
//If the request is valid,
//produce an XML string
//containing the requested information
If($auth == 1) {
// Start the xml document
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"
n";
echo "<Messages>n";
//Connect to the database and select the table
$db = mysql_connect("host","username","password");
mysql_select_db("database");
//Query the table for details about the message
$res = mysql_query("SELECT `fname`, `lname`, `email`, `date_submitted`, `time_submitted`,`MsgID` FROM `tblMessages` WHERE `Status` = ".$_POST['view']." ORDER BY `date_submitted`, `time_submitted`");
//If information exists, write it out as XML
If($rs = mysql_fetch_row($res)) {
do {
echo "<Message>n";
//This is the table name
echo "<MsgID>".$rs[5]."</MsgID>n";
//Field 1
echo "<From>".$rs[0]." ".$rs[1]."</From>n";
//Field 2
echo "<Email>".$rs[2]."</Email>n";
//Field 3
echo "<MsgDate>".$rs[3]."</MsgDate>n";
//Field 4
echo "</MsgTime>".$rs[4]."</MsgTime>n";
//Field 5
echo "</Message>n";
} while($rs = mysql_fetch_row($res));
mysql_close();
}
Echo "</Messages>";
}
? >
Next: What's Said and Done in the End >>
More Visual Basic.NET Articles
More By Nicholas Clayton