通过PHP解析Dictionary对象并创建MySQL表

通过PHP解析Dictionary对象并创建MySQL表,php,mysql,parsing,dictionary,Php,Mysql,Parsing,Dictionary,目前,我的PHP脚本中有一个dictionary对象,它通过我的iOS应用程序发布到我的PHP实现的服务器上: {"command":"save","classname":"GameScore","cheatMode":"0","playerName":"SeanPlott","score":"1337"} 现在我需要根据数据创建一个MySQL表 这是我解析数据的尝试,但我得到的只是对象,而不是键 foreach( $text as $stuff ) { if( is_array( $

目前,我的PHP脚本中有一个dictionary对象,它通过我的iOS应用程序发布到我的PHP实现的服务器上:

{"command":"save","classname":"GameScore","cheatMode":"0","playerName":"SeanPlott","score":"1337"}
现在我需要根据数据创建一个MySQL表

这是我解析数据的尝试,但我得到的只是对象,而不是键

foreach( $text as $stuff ) {
    if( is_array( $stuff ) ) {
        echo json_encode($stuff);
        foreach( $stuff as $thing ) {
            echo json_encode($thing);
        }
    } else {
        echo json_encode($stuff);
    }
}
结果如下<代码>“保存”“游戏分数”“0”“SeanPlott”“1337”

这就是我打算提出的问题

$result = query("INSERT INTO '%s'('%s', '%s', '%s', '%s') 
                    VALUES('%s','%s','%s','%s')", $classname, $key1, $key2, $key3, $key4, 
                                                    $object1, $object2, $$object3, $object4);
但是查询应该是动态的,因为dictionary对象可以有1个或多个键/对象

所以我想我需要实现一个解析for循环,它在数组中生成查询并输出一个字符串,这就是查询

有人知道如何修复我的解析器吗

编辑:这是我处理MYSQL的查询函数

function query() {
    global $link;
    $debug = false; 
    $args = func_get_args();
    $sql = array_shift($args);

    for ($i = 0; $i < count($args); $i++) {
        $args[$i] = urldecode($args[$i]);
        $args[$i] = mysqli_real_escape_string($link, $args[$i]);
    }

    $sql = vsprintf($sql, $args);

    if ($debug) print $sql;

    $result = mysqli_query($link, $sql);

    if (mysqli_errno($link) == 0 && $result) {  
        $rows = array();

        if ($result !== true)
            while ($d = mysqli_fetch_assoc($result)) 
                array_push($rows,$d);

        return array('result' => $rows);

    } else {
        return array('error' => 'Database error');
    }
}
如果有人想办法优化它或者让它更干净。。。请随意发布一些东西


再次感谢大家的投入

您可以使用一个简单的
json\u decode()
内爆()
来格式化它并准备插入。考虑这个例子:

$raw_data = '{"command":"save","classname":"GameScore","cheatMode":"0","playerName":"SeanPlott","score":"1337"}';
$data = json_decode($raw_data, true);
$columns = array_keys($data); // get the columns

$final_statement = "INSERT INTO `table` (".implode(', ', $columns).") VALUES ('".implode("','", $data)."')";
echo $final_statement;
// outputs: INSERT INTO `table` (command, classname, cheatMode, playerName, score) VALUES ('save','GameScore','0','SeanPlott','1337')
$raw_data = '{"command":"save","classname":"GameScore","cheatMode":"0","playerName":"SeanPlott","score":"1337"}';
$data = json_decode($raw_data, true);
$columns = array_keys($data); // get the columns

$values = array();
foreach($data as $key => $value) {
    // if not numeric, add quotes, if not, leave it as it is
    $values[] = (!is_numeric($value)) ? "'".$value."'" : $value;
}

$final_statement = "INSERT INTO `table` (".implode(', ', $columns).") VALUES (".implode(', ', $values).")";
echo $final_statement;
// outputs: INSERT INTO `table` (command, classname, cheatMode, playerName, score) VALUES ('save', 'GameScore', 0, 'SeanPlott', 1337)
// Note: numbers has no qoutes
编辑:假设列具有int类型(尤其是具有数值的列)。无论出于什么原因,它都不起作用,因为不匹配,如果是这样,您可能会这样做。考虑这个例子:

$raw_data = '{"command":"save","classname":"GameScore","cheatMode":"0","playerName":"SeanPlott","score":"1337"}';
$data = json_decode($raw_data, true);
$columns = array_keys($data); // get the columns

$final_statement = "INSERT INTO `table` (".implode(', ', $columns).") VALUES ('".implode("','", $data)."')";
echo $final_statement;
// outputs: INSERT INTO `table` (command, classname, cheatMode, playerName, score) VALUES ('save','GameScore','0','SeanPlott','1337')
$raw_data = '{"command":"save","classname":"GameScore","cheatMode":"0","playerName":"SeanPlott","score":"1337"}';
$data = json_decode($raw_data, true);
$columns = array_keys($data); // get the columns

$values = array();
foreach($data as $key => $value) {
    // if not numeric, add quotes, if not, leave it as it is
    $values[] = (!is_numeric($value)) ? "'".$value."'" : $value;
}

$final_statement = "INSERT INTO `table` (".implode(', ', $columns).") VALUES (".implode(', ', $values).")";
echo $final_statement;
// outputs: INSERT INTO `table` (command, classname, cheatMode, playerName, score) VALUES ('save', 'GameScore', 0, 'SeanPlott', 1337)
// Note: numbers has no qoutes

你想在你的循环中做什么?您的输入看起来像json,所以只需解码一次就可以满足您的所有需要。请注意,您需要白名单来验证表名和列名,以及为值准备的语句。。。目前我只是在回显这些物体。。不是钥匙。。。我发布了我的查询函数来处理向mysqlI插入新表的问题,但我看不出问题出在哪里,
json\u decode()
foreach($var as$key=>$value)
就是您所需要的。我能够解决问题。。。但是这个答案要简单得多,并且很好地使用了PHP函数。。。谢谢你@kevinabelita!我能否就如何解决值内的间距问题提供最新答案?例如,SeanPlott实际上应该是Sean Plott,MySQL向我抛出了一个错误。。。我想我能解决,但一旦我解决了。。。希望看到更好的解决方案:)@jsetting32我有一个关于插入数据类型的编辑,可能很有用。关于尚未创建的表的问题如何?我希望在必要时动态创建一个表。有什么想法吗?@jsetting32一个尚未创建的表出现问题是什么意思?当然,您可以动态创建一个具有不同列名的表,但是长度和类型,您必须手动输入它们,我的推理是,您如何区分哪一个是int,哪一个是仅具有列名的varchar