Search code examples
mysqlactionscript-3flash

How to select first 10 values from database and store it In PHP vars


I have database table named: "Eurokos" and here are 2 columns "name" and "time". I need to make TOP 10 players table in php, who passed game fastest, this mean I need to take lowest time.

To insert data to database I used MySQLi, to select I need to use the same? I've tried $mysqli = new mysqli("localhost","my_db","pass","my_db"); if ($stmt = $mysqli->prepare("SELECT * FROM eurokos e ORDER BY time DESC LIMIT 0, 5"))

Could you help me? I don't know how to echo It, how to store it in vars? Best way if possible would be, get "Top 10" players, get It in php and send to Action Script.

Thank you.

UPDATE: I use this code in action script, but It doesn't work. How I finally can print answer (not trace)? I need help with myButton usage. I see here not printing _message = myXml.user.*; message_txt.text = _message;

var myXml:XML;

function uploadTops():void
{
    var myLoader:URLLoader = new URLLoader();
    myLoader.load(new URLRequest("top.php"));
    myLoader.addEventListener(Event.COMPLETE, processXML);
}
function processXML(e:Event):void {
myXml = new XML(e.target.data);
trace(myXml);
}
function myButton(e:MouseEvent):void
{
    uploadTops();
//  _message = myXml.user.*;
//  message_txt.text = _message;
}

I got error: Error #1090: XML parser failure: element is malformed. when trying to trace

at MemoryGame/processXML()[C:\Users\Petras\Downloads\geraseaa\geraseaa\gerase\MemoryGame.as:570]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

Solution

  • <?php
        header("Content-type: text/xml");
        $mysqli = new mysqli("localhost","my_db","pass","my_db");
        $query = "SELECT name,time FROM eurokos ORDER by time DESC LIMIT 10";
    
        echo '<?xml version="1.0" encoding="UTF-8"?>';
        echo '<results>';
        if ($result = $mysqli->query($query)) {
            while ($row = $result->fetch_assoc()) {
               echo '<user name="'.$row["name"].'" time="'.$row["time"].'" />';
            }
            $result->free();
        }
        echo '</results>';
        $mysqli->close();
    ?>
    

    That will output an XML document that you can load and read in AS :

    <?xml version="1.0" encoding="UTF-8"?>
      <results>
        <user name="someone" time="xxx" />
        <user name="someone" time="xxx" />
        [...]
    </results>
    

    XML is created on the fly every time you call the PHP script from AS.