Ultrashock Forums > Flash > Data Communication
FLASH - PHP - MySQL

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
FLASH - PHP - MySQL
Old 2009-03-06

I have been developing a flash trivia using some tutorial. Everything went fine & I have added some new features to the trivia. The tutorial provides you with a url link to their database in order for you to test your trivia. They used ASP.NET as a way to load & display questions from their DB. Now I should create my own trivia questions & answers and store them in my own DB. The tutorial teaches you how to develop the flash trivia but they didn't mention how to make the ASP.NET file to link the DB to the flash trivia. As I never used ASP.NET before, I thought I would use PHP & MySQL instead. I created my own DB, inserted some random questions & answers and wrote the php code. When I try to test the flash trivia using my DB & the php file I have wrote, it doesn't seem to work. The flash trivia file is working normally but the questions & answers are not loaded from the MySQL DB.

I have included below the ActionScript I use (Working Fine) Plus PHP file (doesn't seem to be working) & screen shots of the MySQL DB.

ActionScript -Frame 1-
Code:
//Stop the timeline
stop();

//Determine the number of questions in the database
var numQCount:Number;
//Create trivia data receiver objects
var varTriviaCount:LoadVars = new LoadVars();
//Load the data from the external web application
varTriviaCount.load("http://localhost/FlashDB/gettriviaquestioncount.php");

//Once the data loads from remote server, get the count
varTriviaCount.onLoad = function(blnSuccess:Boolean):Void {
//See if data was loaded
if(blnSuccess){
//Parse the data returned
numQCount = varTriviaCount.qCount;
} else {
//Display an error
trace("Error occurred on LoadVars");
}
};

//When the Faceoff button is clicked, go to the trivia game
btnLaunch.onRelease = function():Void {
gotoAndStop(10);
};
ActionScript -Frame 10-
Code:
//Initialize trivia variables
var numCurrentQ:Number = 1;
var numCurrentScore:Number = 0;
var numTimerVal:Number = 10;
var numRandomQ:Number;
var strCurrentQ:String;
var arrQuestionList:Array = new Array();
var strAnswerList:String;
var strCorrectList:String;
var strAnswerText:String;
var strCorrectAnswer:String;
var arrAnswerText:Array = new Array();
var arrCorrectAnswer:Array = new Array();
var numCorrectAnswer:Number;

//Start the trivia quiz
getNextQuestion();

function getNextQuestion(){
//Initialize grades
txtGradeC1._visible = false;
txtGradeC2._visible = false;
txtGradeC3._visible = false;
txtGradeC4._visible = false;
txtGradeW1._visible = false;
txtGradeW2._visible = false;
txtGradeW3._visible = false;
txtGradeW4._visible = false;
//Get a random number to determine current question
numRandomQ = Math.floor(Math.random()*numQCount-1) + 1;
//Determine if this question has been asked
var blnNewQuestion:Boolean = true;
//See if this is the first question
if(numCurrentQ!=1){
//This is not the first question
for(z=0;z<arrQuestionList.length;z++){
if(numRandomQ==arrQuestionList[z]){
//This question has already been asked
blnNewQuestion = false;
}
}
}
//See if this question has been asked
if(blnNewQuestion){
//Add the current question number to the list of asked questions
arrQuestionList.push(numRandomQ);
//Create trivia data sender & receiver objects
var varTriviaSend:LoadVars = new LoadVars();
var varTriviaReceive:LoadVars = new LoadVars();
//Load the data from the external web application
varTriviaSend.questionNum = numRandomQ;
//varTriviaSend.send("http://localhost/FlashDB/gettriviaquestion.php","_blank","GET");
varTriviaSend.sendAndLoad("http://localhost/FlashDB/gettriviaquestion.php",varTriviaReceive,"GET");

//Once the data loads from remote server, build the question/answer display
varTriviaReceive.onLoad = function(blnSuccess:Boolean):Void {
//See if data was loaded
if(blnSuccess){
//Parse the data returned
//Set the question and answer text
strCurrentQ = varTriviaReceive.currentQ;
txtQuestion.text = strCurrentQ;
//Set the answers
strAnswerList = varTriviaReceive.answerList;
arrAnswerText = strAnswerList.split("|");
txtAnswer1.text = arrAnswerText[0];
txtAnswer2.text = arrAnswerText[1];
txtAnswer3.text = arrAnswerText[2];
txtAnswer4.text = arrAnswerText[3];
//Unhide the buttons
btnAnswer1._visible = true;
btnAnswer2._visible = true;
btnAnswer3._visible = true;
btnAnswer4._visible = true;
//Unhide the incorrect answers
txtAnswer1._visible = true;
txtAnswer2._visible = true;
txtAnswer3._visible = true;
txtAnswer4._visible = true;
//Determine the correct answer
strCorrectList = varTriviaReceive.correctList;
arrCorrectAnswer = strCorrectList.split("|");
for(y=0;y<arrCorrectAnswer.length;y++){
if(arrCorrectAnswer[y]==1){ numCorrectAnswer = y+1; }
}
} else {
//Display an error
trace("Error occurred on LoadVars");
}
}
//Set the question number/score display
txtQNum.text = numCurrentQ;
txtScore.text = numCurrentScore;
txtTimer.text = numTimerVal;
btnNext._visible = false;
//Run the countdown timer
countDown = function () {
numTimerVal--;
txtTimer.text = numTimerVal;
if (numTimerVal == 0) {
clearInterval(timer);
noAnswer();
btnNext._visible = true;
}
};
timer = setInterval(countDown, 1000);
} else {
getNextQuestion();
}
}

//When the user clicks an answer button, determine if it is correct
btnAnswer1.onRelease = function():Void {
//stop the timer
clearInterval(timer);
//Unhide the 'Next' button
btnNext._visible = true;
//See if answer is correct
if(numCorrectAnswer == 1){
//Answer is correct
numCurrentScore = numCurrentScore + (numTimerVal * 100);
txtScore.text = numCurrentScore
//Set the grades
txtGradeC1._visible = true;
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Hide the incorrect answers
txtAnswer2._visible = false;
txtAnswer3._visible = false;
txtAnswer4._visible = false;
} else {
//Answer is not correct
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Set the grades
txtGradeW1._visible = true;
//Hide the incorrect answers
if(numCorrectAnswer != 2){
txtAnswer2._visible = false;
} else {
//Set the grades
txtGradeC2._visible = true;
}
if(numCorrectAnswer != 3){
txtAnswer3._visible = false;
} else {
//Set the grades
txtGradeC3._visible = true;
}
if(numCorrectAnswer != 4){
txtAnswer4._visible = false;
} else {
//Set the grades
txtGradeC4._visible = true;
}
}
}

//When the user clicks an answer button, determine if it is correct
btnAnswer2.onRelease = function():Void {
//stop the timer
clearInterval(timer);
//Unhide the 'Next' button
btnNext._visible = true;
//See if answer is correct
if(numCorrectAnswer == 2){
//Answer is correct
numCurrentScore = numCurrentScore + (numTimerVal * 100);
txtScore.text = numCurrentScore
//Set the grades
txtGradeC2._visible = true;
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Hide the incorrect answers
txtAnswer1._visible = false;
txtAnswer3._visible = false;
txtAnswer4._visible = false;
} else {
//Answer is not correct
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Set the grades
txtGradeW2._visible = true;
//Hide the incorrect answers
if(numCorrectAnswer != 1){
txtAnswer1._visible = false;
} else {
//Set the grades
txtGradeC1._visible = true;
}
if(numCorrectAnswer != 3){
txtAnswer3._visible = false;
} else {
//Set the grades
txtGradeC3._visible = true;
}
if(numCorrectAnswer != 4){
txtAnswer4._visible = false;
} else {
//Set the grades
txtGradeC4._visible = true;
}
}
}

//When the user clicks an answer button, determine if it is correct
btnAnswer3.onRelease = function():Void {
//stop the timer
clearInterval(timer);
//Unhide the 'Next' button
btnNext._visible = true;
//See if answer is correct
if(numCorrectAnswer == 3){
//Answer is correct
numCurrentScore = numCurrentScore + (numTimerVal * 100);
txtScore.text = numCurrentScore
//Set the grades
txtGradeC3._visible = true;
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Hide the incorrect answers
txtAnswer1._visible = false;
txtAnswer2._visible = false;
txtAnswer4._visible = false;
} else {
//Answer is not correct
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Set the grades
txtGradeW3._visible = true;
//Hide the incorrect answers
if(numCorrectAnswer != 1){
txtAnswer1._visible = false;
} else {
//Set the grades
txtGradeC1._visible = true;
}
if(numCorrectAnswer != 2){
txtAnswer2._visible = false;
} else {
//Set the grades
txtGradeC2._visible = true;
}
if(numCorrectAnswer != 4){
txtAnswer4._visible = false;
} else {
//Set the grades
txtGradeC4._visible = true;
}
}
}

//When the user clicks an answer button, determine if it is correct
btnAnswer4.onRelease = function():Void {
//stop the timer
clearInterval(timer);
//Unhide the 'Next' button
btnNext._visible = true;
//See if answer is correct
if(numCorrectAnswer == 4){
//Answer is correct
numCurrentScore = numCurrentScore + (numTimerVal * 100);
txtScore.text = numCurrentScore
//Set the grades
txtGradeC4._visible = true;
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Hide the incorrect answers
txtAnswer1._visible = false;
txtAnswer2._visible = false;
txtAnswer3._visible = false;
} else {
//Answer is not correct
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Set the grades
txtGradeW4._visible = true;
//Hide the incorrect answers
if(numCorrectAnswer != 1){
txtAnswer1._visible = false;
} else {
//Set the grades
txtGradeC1._visible = true;
}
if(numCorrectAnswer != 2){
txtAnswer2._visible = false;
} else {
//Set the grades
txtGradeC2._visible = true;
}
if(numCorrectAnswer != 3){
txtAnswer3._visible = false;
} else {
//Set the grades
txtGradeC3._visible = true;
}
}
}

//Set the display when no answer is selected
function noAnswer(){
//Hide all the answer buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Hide the incorrect answers
if(numCorrectAnswer != 1){
txtAnswer1._visible = false;
} else {
txtGradeC1._visible = true;
}
if(numCorrectAnswer != 2){
txtAnswer2._visible = false;
} else {
txtGradeC2._visible = true;
}
if(numCorrectAnswer != 3){
txtAnswer3._visible = false;
} else {
txtGradeC3._visible = true;
}
if(numCorrectAnswer != 4){
txtAnswer4._visible = false;
} else {
txtGradeC4._visible = true;
}
}

//When the user clicks the 'Next' button, go to the next question
btnNext.onRelease = function():Void {
//See if this is the last question
if(numCurrentQ < 10){
//Not the last question, pull next question
numCurrentQ++;
numTimerVal = 10;
getNextQuestion();
//Erase the grades
txtGradeC1._visible = false;
txtGradeC2._visible = false;
txtGradeC3._visible = false;
txtGradeC4._visible = false;
txtGradeW1._visible = false;
txtGradeW2._visible = false;
txtGradeW3._visible = false;
txtGradeW4._visible = false;
} else {
//This is the last question, jump to frame 20
gotoAndStop(20);
}
}


PHP Files
1. DB.php
Code:
<?php 
$host = 'localhost'; 
$user = 'username';
$pass = 'password';
$database = 'databasename';
?>
2.
Code:
<?php 


include("DB.php"); 

$table = 'tblTriviaAnswer';

mysql_connect ($host, $user, $pass); 
mysql_select_db ($database);

$result = mysql_query ( "SELECT * FROM `tbltriviaanswer`"); 
$newresult = mysql_query ("SELECT * FROM `tbltriviaquestion`");	



$i = 1; 

	while ( $i <= 10){

		$row = mysql_fetch_array($result); 
		 
		extract($row); 
		echo "$answerText";
		echo "$correctAnswer";
		$i = $i + 1; 
	}

$j = 1; 

	while ( $j <= 10){

		$row = mysql_fetch_array($newresult); 
		 
		extract($row);
		echo "$questionNum";
		$j = $j + 1;

	}
?>
MySQL DB ScreenShots





One Last thing is the DB ScreenShot that was included with the tutorial:



Do you have any ideas how can I edit the PHP file in order for the trivia to load the questions & answers ?
postbit arrow 3 comments | 999 views postbit arrow Reply: with Quote   
Registered User
gasper000 is offline
seperator
Posts: 1
2009-03-06
gasper000 lives in Egypt
seperator

Ultrashock Member Comments:
the_3rock the_3rock is offline the_3rock lives in Canada 2009-03-09 #2 Old  
Some issues I've once experienced with loading data (sound actually) was that having the event-listener AFTER I call the load() function can actually be problematic. Would be best to have it defined BEFORE your load() execution. I don't garantee that it will fix your problem though! Just something maybe to try.
Reply With Quote  
the_3rock the_3rock is offline the_3rock lives in Canada 2009-03-09 #3 Old  
Also, the method you're using to pass the data from Flash to your PHP script is "GET". That's fine, but it seems you're not using it at all in your PHP script! You can obtain your passed variables... say... questionNum - by using $_GET["questionNum"].

To test your script as standalone (to isolate any problems in your script first), you can open your browser to that page http://localhost/FlashDB/gettriviaquestion.php and pass the questionNum in the URL as well: http://localhost/FlashDB/gettriviaqu...?questionNum=1. That's the beauty of the GET method, easy to test. If you would of chosen POST, then it would of been a little trickier!

Hope this helps!
Reply With Quote  
 grouch 2009-03-19 #4 Old  
hi, recently i have been searching some nice way to communicate with server and here you can find some working example: http://www.viztoolkit.com/2009/03/ho...ate-with-flash
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: