【问题标题】:Parsing variables through PHP with Flash通过 PHP 和 Flash 解析变量
【发布时间】:2013-04-06 12:44:45
【问题描述】:

我正在使用 php 和 mysql 数据库在 flash as3 中聊天。 但是我根本不了解 php,并且在更新消息时遇到了问题。 现在我的 php 文件看起来像这样:

$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;

我的问题是如何循环所有已发送的聊天消息以将它们发送到闪存。 我只能用一个来完成,但我需要加载一大堆。

谢谢

【问题讨论】:

    标签: php mysql actionscript-3 flash chat


    【解决方案1】:

    您正在寻找的是“fetchAll”。请注意,您的代码对 SQL 注入是开放的,很容易通过将恶意值传递给 PHP 脚本来删除您的数据库。因此,我已将代码从已弃用的 Mysql 扩展更改为 PDO。 PDO 将为您转义值。 阅读 PHP 手册中有关 PDO 的更多信息(那里有很多示例)。

    还请注意,您必须修改以下代码,因为我无法猜测数据库中聊天表的字段名称是如何命名的。所以你必须修改下面的插入语句。

    // 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;
    

    Actionscript 将收到一个类似于此的 JSON 对象:

    [
    
       {
    
          "sendtime":"2013-04-14",
    
          "caster":"person1",
    
          "msg":"Message 1"
    
       },
    
       {
    
          "sendtime":"2013-04-15",
    
          "caster":"person2",
    
          "msg":"Message 2"
    
       }
    ]
    

    如您所见,JSON 没有特定的变量名称,例如问题中使用 GET 的版本(问题中使用的方法不适用于大型结果列表)。

    那么您如何在 Actionscript 中处理 JSON 文档?我不是动作脚本程序员,但这篇 Stackoverflow 帖子看起来是对这个问题的合理回答:

    Get and parse JSON in Actionscript

    【讨论】:

    • 好的。但是我现在应该如何获取变量?我不知道他们的名字。
    • 检查“ print json_encode($result); ”而不是 print_r($result) 是否适合您与 Actionscript 结合使用。这会将关联数组转换为 JSON,并且应该很容易使用 Actionscirpt 遍历数组的元素。
    • soooo,我将得到名为结果的数组?
    • 看这里的例子:php.net/manual/pl/pdostatement.fetchall.php - 结构显示在那里。请注意,您可以对 fetchAll 使用不同的选项,这将影响数组的外观)。看看哪一个最适合您的用例。
    • 好的。谢谢。但是你能帮我描述一下数组会取什么名字吗?我真的不明白我应该参考什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多