我正在研究一些数据库函数,由于某种原因,我创建了两个函数来进行简单的插入/选择,并将数组作为参数传递给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中按键被打印,因为它们应该......有什么想法可以在那里进行操作并找到解决方法吗?
答案 0 :(得分:3)
您缺少括号:
$header .= $key . (($cur < $count) ? ', ' : ' ');
像这样它会起作用。