Meetings every second Tuesday
Flex 2.0 with connection to mysql database using php
Hi,
I have created a little application in Flex using FlexBuilder 2.0 for getting some data from a mysql server.
I use a php script for requesting the Mysql server.
Here is the mxml script :
<mx:Script>
<![CDATA[
function logIn():void
{
userRequest.send();
}
function userCount(event:Object):void
{
//mx.controls.Alert.show(event.result.doc.user, 'Callback');
user.text = event.result.doc.user;
}
function httpError(event:Object):void
{
mx.controls.Alert.show(event.toString(), 'Callback');
}
]]>
</mx:Script>
<mx:Form width="200" height="255" id="form1">
<mx:Panel width="128" height="143" layout="absolute" title="Test" id="panel1">
<mx:Text x="0" y="0" text="# user" width="108" textAlign="center"/>
<mx:TextInput x="25" y="20" width="58" height="20" id="user" editable="true" enabled="true" fontSize="10" color="#ff0000" textAlign="center" fontWeight="bold"/>
</mx:Panel>
<mx:Button label="Count" click="logIn()" id="button1"/>
</mx:Form>
<mx:HTTPService id="userRequest" url="functions.php" useProxy="false" method="POST" showBusyCursor="true" result="userCount(event)" fault="httpError(event.fault)"/>
</mx:Application>
I have a "Count" button. When I click on the button, a request is send to the mysql server. The TextInput "user" is updated with the number of user found.
here is the php script in the functions.php:
<?php
$server="localhost";
$user="toto";
$pass="toto";
$db="myDatabase";
$link = mysql_connect($server, $user, $pass);
$ret = mysql_select_db($db, $link);
$query = "SELECT count(*) as nb from user";
$result = mysql_query($query, $link);
$nb=0;
while($row=mysql_fetch_array($result))
{
$nb += $row[0];
}
mysql_free_result($result);
mysql_close($link);
$xml="<?xml version=\"1.0\" encoding=\"utf-8\"?><doc><user>".$nb."</user></doc>?>";
header("Content-Type: text/xml");
print ($xml);
?>
The script works fine only the first time I have open the browser.
When I make change in the mysql server (I add a new user) and I click on the "Count" button, the result is the same as previous...
If a close the browser and open a new one, the result is good.
So, is my method bad for querying the mysql database ? what is wrong ? Is something in the browser cache ?
Thanks in advance
Olivier





Requested critique
I don't see where your script requests a new query to the MySQL database when you click on the "Count". The script you show calls on the original value of the '$nb' variable for information.
Hope this helps.