1

我正在研究一些数据库函数,出于某种原因,我制作了两个函数来制作简单的插入/选择,其中数组作为参数传递给 php 函数。问题从这里开始。

    // basic database functions, to aid the development :D
    static public function Insert($table, $items = array())
    {
        if(is_array($items) == false)
        {
            return false;
        }

        $header = 'INSERT INTO ' . $table . '(';
        $values = 'VALUES(';

        $count = count($items) - 1;
        $cur = 0;

        foreach($items as $key => $value)
        {
            $num = is_numeric($value);

            $header .= $key . ($cur < $count) ? ', ' : ' ';
            $values .= (($num) ? '' : '\'') . $value . (($num) ? '' : '\'') . (($cur < $count) ? ', ' : '');

            $cur ++;
        }

        $header .= ')';
        $values .= ')';

        self::Query($header . $values);
    }

        $pair = array('id' => null,
                      'name' => Database::EscapeString((string)$xml->ID),
                      'avatar_url' => Database::EscapeString((string)$xml->Avatar),
                      'personal_msg' => Database::EscapeString((string)$xml->AboutMe),
                      'level' => (int)$xml->Level,
                      'progress' => (int)$xml->Progress,
                      'trophies_total' => (int)$xml->Trophies->Total,
                      'trophies_platinum' => (int)$xml->Trophies->Platinum,
                      'trophies_gold' => (int)$xml->Trophies->Gold,
                      'trophies_silver' => (int)$xml->Trophies->Silver,
                      'trophies_bronze' => (int)$xml->Trophies->Bronze,
                      'country' => Database::EscapeString((string)$xml->Country->Culture),
                      'is_plus' => (string)$xml->Plus,
                      'points' => (int)$xml->LevelData->Points,
                      'floor' => (int)$xml->LevelData->Floor,
                      'ceiling' => (int)$xml->LevelData->Ceiling,
                      'game_completion' => (double)$xml->GameCompletion,
                      'background_color' => ((int)$xml->Background->R << 16) | ((int)$xml->Background->G << 8) | ((int)$xml->Background->B),
                      'jid' => (string)$xml->jid
                      );

        Insert('profiles', $pair);

查询包含的数据不正确:

INSERT INTO profiles(, , , , , , , , , , , , , , , , , , , )VALUES('', 'AlmamuPP', 'http://static-resource.np.community.playstation.net/avatar/3RD/30004.png', 'EVE Online & DUST fan', 2, 17, 12, 0, 0, 6, 6, 'ES', 'false', 270, 200, 600, 12.5, 458759, 'AlmamuPP@a4.es.np.playstation.net')

您可能会注意到,它只是忽略了字典中的所有键,但有趣的事实是,如果我添加

echo $key;

在 foreach 中,键按应有的方式打印...关于那里可能发生的事情以及解决方法的任何想法?

4

1 回答 1

3

您缺少括号:

$header .= $key . (($cur < $count) ? ', ' : ' ');

像这样它会起作用。

于 2013-02-11T18:31:51.147 回答