Need a simple script that is reusable, easy to figure out and not built into a lengthy frame work?
The sample code provided is a PHP script that connects to a MySQL database and outputs XML for the HTTPService tag in flex to connect to. XML tags will be named the same as your column names in your table. Note: This loads very well up 1,000 records by 36 Columns. Stress tested up to 15000, which will still load to the data grid, but it's a noticeable load time.
1. Setup your MySQL Database.
2. Copy the PHP code below & adjust connection & query information .
3. Open the PHP file in a browser to check XML is displaying correctly (View Source).
4. Copy Flex code below & adjust URLs to point to the PHP script.
5. Adjust the column name in the datagrid to match a column name in the XML.
6. Run the Flex application.
All variables that need to be changed for your setup have been bolded. Just add columns as necessary.
PHP CODE-----------------------------------------------------------------------------------
<?php
//SQL Connection Info - update with your database, username & password
$connection = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die ('cannot reach database');
$db = mysql_select_db("YourDatabaseName") or die ("this is not a valid database");
//Change this query as you wish for single or multiple records
$result = mysql_query("SELECT * FROM yourTableName");
//Get the number of rows
$num_row = mysql_num_rows($result);
//Start the output of XML
echo '<?xml version="1.0" encoding="iso-8859-1"?>';
echo "<data>";
echo '<num>' .$num_row. '</num>';
if (!$result) {
die('Query failed: ' . mysql_error());
}
/* get column metadata - column name -------------------------------------------------*/
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
$ColumnNames[] = $meta->name; //place col name into array
$i++;
}
$specialchar = array("&",">","<"); //special characters
$specialcharReplace = array("&",">","<"); //replacement
/* query & convert table data and column names to xml ---------------------------*/
$w = 0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "<row>";
foreach ($line as $col_value){
echo '<'.$ColumnNames[$w].'>';
$col_value_strip = str_replace($specialchar, $specialcharReplace, $col_value);
echo $col_value_strip;
echo '</'.$ColumnNames[$w].'>';
if($w == ($i - 1)) { $w = 0; }
else { $w++; }
}
echo "</row>";
}
if($num_row == "1"){
echo '<row></row>';
}
echo "</data>";
mysql_free_result($result);
?>
FLEX CODE----------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="doSend()"
layout="absolute">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var datalist:ArrayCollection;
private function resultHandler(event:ResultEvent):void{
datalist = event.result.data.row;
rowcount = event.result.data.numrows;
if(rowcount == '1'){
datalist.removeItemAt(1);
}
}
public function doSend():void {
xmlFromDatabase.url = "http://localhost/xml.php";
xmlFromDatabase.send();
}
]]>
</mx:Script>
<mx:HTTPService url="http://localhost/xml.php"
id="xmlFromDatabase"
showBusyCursor="true"
result="resultHandler(event)"
method="POST" />
<mx:DataGrid x="10" y="10" dataProvider="{datalist}" id="dg">
<mx:columns>
<mx:DataGridColumn headerText="Col1" dataField="Column1Name"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>