0

I'm making chat in flash as3 with php and mysql database. However I don't know php at all, and got problem with updating messages. for now my php file looks like this:

$caster = $_POST['caster'];
$msgText = $_POST['msgText'];
$sendTime = $_POST['sendTime'];

$query = "INSERT INTO chat VALUES ('','$sendTime','$caster','$msgText')"
mysql_query($query);
$query="SELECT * FROM chat";
$result=mysql_query($query);
$cast=mysql_result($result,1,"caster");
mysql_close();

$returnVars = array();
$returnVars['success'] = $success;
$returnVars['caster'] = $cast;
$returnString = http_build_query($returnVars);
echo $returnString;

And my question is how to loop for all already sent chat messages to send them to flash. I can only do it with one, but I need whole bunch of them to be loaded.

Thanks

Krzysztof Wende
  • 3,208
  • 25
  • 38

1 Answers1

0

What you are looking for is "fetchAll". Note that your code is open to SQL injection, it is very easy to drop your database by passing evil values to the PHP script. I have changed the code therefore from the deprecated Mysql extension to PDO. PDO will to the escaping of the values for you. Read more on PDO in the PHP manual (Lots of examples over there).

Also note that you have to adapt the following code snipped as I could not guess how the field names of the chat table in your database are named. So you have to adapt the insert statement below.

// database config parameters
$dbhost = "localhost";
$dbname = "test";
$dbuser = "root";
$dbpass = "";


try {

    // try to set up a db connection
    $db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

    // insert the data using PDO's prepared statements
    // you have to adapt this line to the field names of your chat table!!
    $sql = "INSERT INTO chat (sendtime,caster,msg) VALUES (:sendtime,:caster,:msg)";
    $sth = $db->prepare($sql);
    $sth->execute(array(
         ':caster' => $_POST['caster'],
         ':sendtime' => $_POST['sendTime'],
         ':msg' => $_POST['msgText']
    ));


    // Get everything
    $sth = $db->prepare("SELECT * FROM chat");
    $sth->execute();
    $result = $sth->fetchAll(PDO::FETCH_ASSOC);
    // your code to format and return the data goes here 
    print json_encode($result);
}
catch (PDOException $e) {
    // if anything related to the database goes wrong, catch the exceptions
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

$db = null;

The Actionscript will receive a JSON object looking similar to this:

[

   {

      "sendtime":"2013-04-14",

      "caster":"person1",

      "msg":"Message 1"

   },

   {

      "sendtime":"2013-04-15",

      "caster":"person2",

      "msg":"Message 2"

   }
]

As you can see the JSON has no specific variable name like in the version with GET used in the question (the method used in the question does not work for large result lists).

So how do you work with the JSON document in Actionscript? I am not an actionscript programmer, but this Stackoverflow post looks like a reasonable answer to this problem:

Get and parse JSON in Actionscript

Community
  • 1
  • 1
herrjeh42
  • 2,782
  • 4
  • 35
  • 47
  • Ok. But how am i supposed to get the variables now? I dont know their names. – Krzysztof Wende Apr 14 '13 at 12:47
  • check if " print json_encode($result); " instead of print_r($result) works for you in combination with Actionscript. This will convert the associative array to JSON and it should be easy to iterate through the elements of the array with Actionscirpt. – herrjeh42 Apr 14 '13 at 13:22
  • soooo, I will get array named result in as? – Krzysztof Wende Apr 14 '13 at 14:16
  • look at the examples here: http://php.net/manual/pl/pdostatement.fetchall.php - The structure is shown there. Note that you can use different options with fetchAll which will influence how the array looks like). Check out which one fits best to your use case. – herrjeh42 Apr 14 '13 at 14:38
  • Ok. Thanks. But can you describe for me what name will array take? I really dont get what should i refer to – Krzysztof Wende Apr 14 '13 at 15:40
  • It has no name - it's a document containing the array in JSON notation. Check out my update to the answer and the linked article how to receive JSON in Actionscript. – herrjeh42 Apr 14 '13 at 16:18