【问题标题】:Concatenating PHP strings from an iteration从迭代中连接 PHP 字符串
【发布时间】:2013-01-26 22:14:17
【问题描述】:

我正在处理一些数据库函数,出于某种原因,我制作了两个函数来进行简单的插入/选择,其中数组作为参数传递给 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 中,键按应有的方式打印...您知道那里可能发生什么以及解决方法吗?

【问题讨论】:

  • 您的代码极易受到 SQL 注入的攻击
  • 如果你还没有注意到,数组中的每一个数据都已经被一些mysql函数(Database::EscapeString)转义了。
  • 猜测三元运算符和连接运算符之间的优先顺序不是您所期望的。试试看:$header .= $key; ($cur &lt; $count) ? $header.=', ' : $header.='';
  • $xml-&gt;Plus$xml-&gt;jid 除外
  • 您是否尝试过将表达式包装在() 上?喜欢$header .= $key . (($cur &lt; $count) ? ', ' : ' ');

标签: php string dictionary iteration concatenation


【解决方案1】:

你缺少括号:

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

这样就可以了。

【讨论】:

  • 你是对的。 Philip Whitehouse 提出的解决方案也有效。我想我必须习惯这样的 PHP 东西 ._.
猜你喜欢
  • 2018-07-25
  • 2011-06-03
  • 2015-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-21
  • 2014-04-04
  • 2019-12-07
相关资源
最近更新 更多