View Full Version : Make function wait for MySQL
psyfer88
September 7th, 2007, 07:49 AM
I am new at AS3...
I got an example of ASQL from http://asql.mooska.pl/
Now I am trying to create a function that can be called and return the Data from the database.
Problem is the function returns a value before the SQL is loaded.
Can anyone help? Here is the code:
package src{
import flash.display.Sprite;
import flash.events.*;
import src.mysql_query;
public class logon_main extends Sprite {
private var Data:Array = new Array;
private var my_query:String = new String;
public function logon_main() {
btnSubmit_1.addEventListener(MouseEvent.CLICK, btnSubmitclick);
}
function btnSubmitclick(m:MouseEvent):void {
my_query = ( "SELECT * FROM users u where userName='"+edtName_1.text+"'" );
Data = mysql_query.getData(my_query);
trace(Data[0].passWord);
}
}
}
package {
import pl.mooska.asql.*;
import pl.mooska.asql.events.*;
public class mysql_query {
public function mysql_query() {
}
public static function getData(my_query:String):Array {
var connector:Asql=new Asql;//creating instance of asql
var Data:Array=new Array;
connector.addEventListener(SQLEvent.CONNECT,handle Connect);
connector.addEventListener(SQLError.SQL_ERROR,hand leError);
connector.addEventListener(SQLEvent.SQL_DATA,handl eData);
connector.connect("MySQL-Server","User","Password","db_name",3306);//connecting to the server //arguments are host, user, pass, database, and port
function handleConnect(evt:SQLEvent):void {
trace("ASQL is connected");
connector.query(my_query);//The query
}
function handleError(evt:SQLError):void {
trace("Error catched " + evt.text);
}
function handleData(evt:SQLEvent):void {
trace("Final data received");
Data = evt.data;
connector.disconnect();
}
return Data;
}
}
}
Daganev
September 7th, 2007, 01:13 PM
wait for the COMPLETE event.
Psaldorn
September 7th, 2007, 04:55 PM
You might want to look into AMFPHP/Flash remoting. Lets you call PHP classes and handle teh responses (like Ajax.Request in JavaScript)
http://www.oscartrelles.com/archives/as3_flash_remoting_example
http://www.amfphp.org
I found it a little hard to get into but it has the advantage of locking all the SQL away in PHP files, so people can't read it by decompiling the swf.
Dazzer
September 8th, 2007, 11:52 AM
Unfortunately SWX isn't available for AS3.0, so yes AMFPHP seems the way to go :)
psyfer88
September 9th, 2007, 02:09 PM
Hi,
Thank u for the replies.
I am not really worried about someone trying to decompile my SWF file.
This is only a small part of a much bigger experiment(in a secure environment).
I just want an easy way to get data from mySQL...
What my question boils down to is this...:
http://www.kirupa.com/forum/showthread.php?t=274743
Thanks again!
:}
joshchernoff
September 9th, 2007, 09:42 PM
The AMF protocol is just faster hands down! Stick with a remoting frame work and use any of the following. amfphp via php, coldfusion via coldfusion, openamf via J2EE, AMF.net via .net... Bottom line is theres a reason why people have let other sever side scipts manage the back end. It's faster, more secure, effective and most of all it's a common practice. You will find others out there with help and code to share for common tasks.
joshchernoff
September 9th, 2007, 10:26 PM
I make extensive use of dannypatterson's remoting frame work.
this class I show here is a simple way to check if user name and user password exist in a database as a way to login.
type's of events that you can respond to
onConnectionError: (trace out error).
onLogin: (on "success" or "fail").
onFault: (trace out error).
callLogin: (trace out when event starts).
sample use of this class
import com.gfxcomplex.auth.AccountSystem;
var user:AccountSystem = new AccountSystem();
user.login("username":String, "userpass":String);
Please note I have not implemented a custom eventDispatcher so the example above is incomplete but can be easily fixed. As for testing and learning I do trace out the events as they happen in the output panel so you still see it working.
AccountSystem class.
package com.gfxcomplex.auth{
import flash.events.EventDispatcher;
import com.dannypatterson.remoting.FaultEvent;
import com.dannypatterson.remoting.ResultEvent;
import com.dannypatterson.remoting.ServiceProxy;
public class AccountSystem extends EventDispatcher{
private const _gateWayURL:String = "http://www.["your URL"].com/amfphp/gateway.php";
private const _servicePath:String = "[your service's name and dir]";
private var _userName:String;
private var _userPass:String;
private var _rsHolder:Object
private var serviceProxy:ServiceProxy;
//I made the login a method so that you can choose when to start it
public function login(userName:String, userPass:String):void{
_userName = userName;
_userPass = userPass;
callLogin();
}
public function AccountSystem() {
//nothing to consturct
}
private function callLogin():void{
serviceProxy = new ServiceProxy(_gateWayURL ,_servicePath);
serviceProxy.addEventListener(ResultEvent.RESULT, onLogin, false, 0, true);
serviceProxy.addEventListener(FaultEvent.FAULT, onFault, false, 0, true);
serviceProxy.addEventListener(FaultEvent.CONNECTIO N_ERROR, onConnectionError, false, 0, true);
serviceProxy.LogIn(_userName, _userPass);
trace("made call");
}
private function onConnectionError(event:FaultEvent):void {
trace("onConnectionError: " + event.fault);
}
private function onFault(event:FaultEvent):void {
trace("onFault: " + event.fault);
}
private function onLogin(event:ResultEvent):void {
var return_result = event.result;
switch(return_result.status){
case "fail":
trace("fail");
break;
case "success":
trace("success");
trace(return_result.Results.serverInfo.initialData[0]);
break;
}
}
}
}Heres a PHP services I use with AMFPHP that will return ID, Name and type of Rights the use has with "success" as a status or if there is no user a "fail" as a status.
<?
include("config.php"); //Include database info for databaseUsername and such...
class accountManager {
function accountManager() {
$this->methodTable = array(
"LogIn" => array(
"description" => "login User",
"access" => "remote",
"arguments" => array ("Username", "Password")
)
);
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS);
$db = mysql_select_db(DB_NAME);
}
function LogIn($Username, $Password){
$Username = escape($Username);
$Password = md5(trim($Password));
$query = "SELECT Acct_ID, Acct_Name, Acct_Rights FROM User_Accts WHERE Acct_Name = '$Username' AND Acct_Pass = '$Password'";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if($num_rows == 1){
return array("status" => "success", "Results" => $result);
} else {
return array("status" => "fail");
}
}
function escape($string) {
return mysql_real_escape_string(htmlspecialchars($string) );
}
}
?>
psyfer88
September 10th, 2007, 12:46 PM
Thanks joshchernoff!
I have never worked with php... but will give it a try.
Psaldorn
September 11th, 2007, 06:44 AM
To both previous posters,
When using AMFPHP I found it very useful to use Cristophe Herreman's MethodTable automation, found at http://www.herrodius.com/blog/?p=4
mooska
September 17th, 2007, 06:28 AM
to make it properly with both remoting or any socket class, you would need to understand how to handle asynchronous result
package {
import flash.display.Sprite;
import flash.events.*;
import flash.text.TextField;
import MysqlQuery;
public class Main extends Sprite {
private var Data:Array = new Array;
private var my_query:String = new String;
public var btnSubmit_1:Sprite;
public var edtName_1:TextField;
public function Main()
{
btnSubmit_1.addEventListener(MouseEvent.CLICK, btnSubmitclick);
}
function btnSubmitclick( m:MouseEvent ):void {
my_query = ( "SELECT * FROM users u where userName='"+edtName_1.text+"'" );
MysqlQuery.getData(my_query, logon);
}
private function logon( data:Object ) :void
{
trace("result received " +data[0] );
}
}
}
package {
import pl.mooska.asql.*;
import pl.mooska.asql.events.*;
public class MysqlQuery {
public function MysqlQuery() {
}
public static function getData(my_query:String, handler:Function ):Array {
var connector:Asql=new Asql;//creating instance of asql
var Data:Array=new Array;
connector.addEventListener(SQLEvent.CONNECT,handle Connect);
connector.addEventListener(SQLError.SQL_ERROR,hand leError);
connector.addEventListener(SQLEvent.SQL_DATA,handl eData);
connector.connect( "localhost", "root", "mooska", "flash" );//connecting to the server //arguments are host, user, pass, database, and port
function handleConnect(evt:SQLEvent):void {
trace("ASQL is connected");
connector.query(my_query);//The query
}
function handleError(evt:SQLError):void {
trace("Error catched " + evt.text);
}
function handleData(evt:SQLEvent):void {
trace("Final data received");
connector.disconnect();
handler( evt.data );
}
}
}
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.