FLASH - PHP - MySQL

Discussion in 'PHP' started by gasper000, Mar 7, 2009.

  1. #1
    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-
    //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);
    };
    Code (markup):
    ActionScript -Frame 10-
    //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);
    }
    }
    Code (markup):


    PHP Files
    1. DB.php
    <?php 
    $host = 'localhost'; 
    $user = 'username';
    $pass = 'password';
    $database = 'databasename';
    ?>
    Code (markup):
    2.
    <?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;
    
    	}
    ?>
    Code (markup):
    MySQL DB ScreenShots
    [​IMG]

    [​IMG]


    One Last thing is the DB ScreenShot that was included with the tutorial:
    [​IMG]


    Do you have any ideas how can I edit the PHP file in order for the trivia to load the questions & answers ?
     
    gasper000, Mar 7, 2009 IP
  2. gasper000

    gasper000 Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Any hints ?
     
    gasper000, Mar 7, 2009 IP
  3. AndreasB

    AndreasB Peon

    Messages:
    280
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I've tried to use MySQL together with Flash, and I had to use XML to export the information from MySQL via PHP, and then import the XML-file into Flash.
     
    AndreasB, Mar 8, 2009 IP
  4. gasper000

    gasper000 Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There's a way that allows you to make an XML file to import the data into Flash but that requires that the XML file has all the questions & answers which may be not secure because the users can open the XML file & know all the right answers which makes the trivia useless. I think in my case this problem is not major because I have 5000 questions, so even if a user opens the XML file, he/she won't be able to know all the right answers. I just wanted to make everything the best way possible but it seems it's really hard to do that. Do you have any idea how to make a multiplayer flash game ?
     
    gasper000, Mar 8, 2009 IP